0

Here is my situation. I have the following array.

arrParent = [[List1,A,B,C],[List2,E,F,G,H]];
arrChild1 = [List1,A,B,C];
arrChild2 = [List2,E,F,G,H];

I want to add "D" to the end of arrChild1. I am having difficulty targeting the child array in my javascript.

I am using:

arrParent[0].push("D")

...but the result is not what I want. The result I get is:

arrChild[(List1,A,B,C),D];

I can verify this my calling:

console.log(arrChild1[0] (which displays List1,A,B,C)
console.log(arrChild1[1] (which displays D)

I need:

arrChild1[0] = List1
arrChild1[1] = A
arrChild1[2] = B
arrChild1[3] = C
arrChild1[4] = D

Can someone point me in the right direction? Thanks in advance.

7
  • 2
    Are you sure you don't have arrChild1 = [[List1, A, B, C]]? Commented Aug 11, 2017 at 20:29
  • Your code should work. Can you try to find more depth in your problem? Commented Aug 11, 2017 at 20:33
  • I have test this var arrParent = [['List1','A','B','C'],['List2','E','F','G','H']]; arrParent[0].push('D'); console.log(arrParent[0]); The result was as expected: ["List1", "A", "B", "C", "D"] Commented Aug 11, 2017 at 20:35
  • Code works for me. Commented Aug 11, 2017 at 20:36
  • To all: Thanks for looking at this. While I don't have the answer just yet I have narrowed the cause to be how the array is being dynamically created using .push() in other functions. I need to look at them. Commented Aug 11, 2017 at 20:53

1 Answer 1

1
var foo = [1,2,3]; // access like: foo[0] === 1

var bar = [foo, [4,5,6]];
var baz = bar[0]; // baz[0] === 1 same as foo[0]

//so to access an array within array
var fuz = bar[0][0]; // fuz === 1
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.