admin管理员组文章数量:1327843
I would like to create an object like this:
"store1":
{
"isbn":"3129321903",
"title":"here es the title",
"author":"author of the book"
},
{
"isbn":"3333333333",
"title":"title of second book",
"author":"author of second book"
}
"store2":
{
"isbn":"3333311111",
"title":"title of book from store2",
"author":"author of book from store2"
}
I tried something like this:
var storeArray = [];
for(var i = 0; i < response.data.bookInfo.length; i++){
var keys = Object.keys(response.data.bookInfo[i]);
var storename = keys.filter(key => key != 'isbn' && key != 'metadata'); // get storename
if(storeArray[storename] == undefined){
storeArray[storename] = response.data.bookInfo[i];
}
else{
storeArray[storename].push(response.data.bookInfo[i]);
}
}
But this just gets me the following error:
TypeError: storeArray["amazon"].push is not a function
When I use storeArray[storename] = response.data.bookInfo[i]
, it only shows me one row in console, I would like to add the response data to storeArray[storename] to get a list of books by store.
I would like to create an object like this:
"store1":
{
"isbn":"3129321903",
"title":"here es the title",
"author":"author of the book"
},
{
"isbn":"3333333333",
"title":"title of second book",
"author":"author of second book"
}
"store2":
{
"isbn":"3333311111",
"title":"title of book from store2",
"author":"author of book from store2"
}
I tried something like this:
var storeArray = [];
for(var i = 0; i < response.data.bookInfo.length; i++){
var keys = Object.keys(response.data.bookInfo[i]);
var storename = keys.filter(key => key != 'isbn' && key != 'metadata'); // get storename
if(storeArray[storename] == undefined){
storeArray[storename] = response.data.bookInfo[i];
}
else{
storeArray[storename].push(response.data.bookInfo[i]);
}
}
But this just gets me the following error:
TypeError: storeArray["amazon"].push is not a function
When I use storeArray[storename] = response.data.bookInfo[i]
, it only shows me one row in console, I would like to add the response data to storeArray[storename] to get a list of books by store.
-
2
Post sample
response.data
– Eddie Commented Apr 10, 2018 at 5:56 -
3
You need
storeArray[storename] = [ response.data.bookInfo[i] ];
so you get an array – Asons Commented Apr 10, 2018 at 5:58 -
1
The structure you defined in your question is not valid. If you meant for each store to contain an array of books, then you need to use array brackets,
[]
. You can't just stick mas in between normal objects. – Sam Axe Commented Apr 10, 2018 at 5:59 -
And you cant use
push
to an object. Your desired output is an object and not an array. – Eddie Commented Apr 10, 2018 at 6:06
2 Answers
Reset to default 5It means that your storeArray[storename]
is not an array.
At
storeArray[storename] = response.data.bookInfo[i];
you just assign a value to the storeArray[storename]
which is not an array. You need first to create an array and then put that element into it.
storeArray[storename] = [ response.data.bookInfo[i] ];
and at the next iterations you will have an array with one element and can use push
on it.
push
is a function of Array
itself. So: storeArray.push(...)
本文标签: arraysJavascript push is not a functionStack Overflow
版权声明:本文标题:arrays - Javascript push is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742220457a2435358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论