3

Hi I want to use nested arrays in my javascript function but it doesn't work. Here is my function:

var arr = [];

function test(id, value){
   arr.push(new Array("id" = id, "value" = value));
}

so as you find out I want to create something like this:

arr[0][id = "example0", value = "value0"];
arr[1][id = "example1", value = "value1"];
arr[2][id = "example2", value = "value2"];
...

1 Answer 1

10

Because new Array("id" = id, "value" = value) is not an array.

You want an array holding an object.

arr.push({"id":id, "value":value});

Read values

console.log(arr[0].id);
Sign up to request clarification or add additional context in comments.

Comments

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.