0
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.

2
  • 2
    secondArray is an object and not an array Commented Dec 11, 2015 at 11:33
  • 1
    {arrayName:x,secondArray: {name:x, value:y}} is object not array..Try data[0].secondArray={name:x, value:y}; or if you want secondArray to be an array then initialize it as an array. ata[0].secondArray.push({name:x, value:y}); will work in that case.. Commented Dec 11, 2015 at 11:33

1 Answer 1

2

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';

Sign up to request clarification or add additional context in comments.

5 Comments

@RayonDabre Actually, it does answer the question as much as possible.
@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..
@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.
The time I posted my first revision, your comment was already there @RayonDabre. I appologize for the inappropiate answer the very first time!!
I am so quick you know :P Never mind bro :) Happy Coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.