javascript - Transfer value of object inside array to another array -
i have object this:
var animals_data = { category : [ { class : "reptiles", animals : [ { image1 : "some image", image2 : "some image 2", name : "snake", description : "some description" }, { image1 : "another image", image2 : "another image 2", name : "crocodilia", description : "another description" }, ] }, { class : "mammals", animals : [ { image1 : "more image", image2 : "more image 2", name : "cat", description : "more description" }, { image1 : "image", image2 : "image 2", name : "dog", description : "long description" } ] }, { class : "birds", animals : [ { image1 : "bird image", image2 : "bird image 2", name : "american flamingo", description : "bird description" }, { image1 : "another bird image", image2 : "another bird image 2", name : "atlantic puffin", description : "another bird description" }, ] } ]};
i want values of firstindex , put them in different array. however, having hard time doing this. code did is:
for (var = 0; < animals_data.category.length; i++) { var currentiteration = animals_data.category[i]; var currentclass = currentiteration.class; animals_array.push(currentclass); };
when run code error says: cannot read property 'firstindex' of undefined. thank help.
edit: included actual object working with. have omitted parts basically, that's gist. values want put in new array ones in "class" property.
solved it. thank xotic750 pointing out mistake. question is:
when used:
for (var = 0; < obj.firstkey.length; i++); { var currentiteration = obj.firstkey[i]; var currentindex = currentiteration.firstindex; new_array.push(currentindex); };
the array doubled (i.e 2 of each added).
however when replaced with
new_array.push(obj.firstkey[i].firstindex;
it runs correctly. may know why there such difference? thank you.
Comments
Post a Comment