data = [];
data.push({arrayName:x,secondArray: {name:x, value:y}});
Now how can you push an item into secondArray ? have tried with
data[0].secondArray.push({name:x, value:y});
But getting error like data[0].secondArray.push() is not a function.
Oleksandr T.
77.6k17 gold badges177 silver badges145 bronze badges
asked Dec 11, 2015 at 11:31
Rijad Rahim
4194 silver badges12 bronze badges
1 Answer 1
This is because secondArray is not a array but an object.
To add properties in an object you can use
secondArray.propertyName = Value;//not to use this syntax if 'propertyName' if not a valid string.
secondArray[propertyName] = Value;
For eg use : data[0].secondArray.name = 'x';
answered Dec 11, 2015 at 11:32
sahil gupta
2,34914 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Erich Kitzmueller
@RayonDabre Actually, it does answer the question as much as possible.
Rayon
@ammoQ, if This is because secondArray is not a array but an object does answer this question according to you then no more arguments I feel question is unclear hence it is better to understand the question to provide better solution..
Erich Kitzmueller
@RayonDabre The first revision of the answer didn't answer the question, you are right. When I came in, there already was the second revision with some code that IMO answers the question as good as possible.
sahil gupta
The time I posted my first revision, your comment was already there @RayonDabre. I appologize for the inappropiate answer the very first time!!
Rayon
I am so quick you know :P Never mind bro :) Happy Coding
Explore related questions
See similar questions with these tags.
lang-js
{arrayName:x,secondArray: {name:x, value:y}}is object notarray..Trydata[0].secondArray={name:x, value:y};or if you wantsecondArrayto be an array then initialize it as anarray.ata[0].secondArray.push({name:x, value:y});will work in that case..