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.

Share asked Apr 10, 2018 at 5:55 Elvira Elvira 1,4405 gold badges26 silver badges54 bronze badges 4
  • 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
Add a ment  | 

2 Answers 2

Reset to default 5

It 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