-6

I just started a couple of weeks ago with javascript. How can I add or editing elements to this array:

var array = [["A1","B1","C1"],
             ["A2","B2","C2"],
             ["A3","B3","C3"],
             ["A4","B4","C4"],
             ["A5","B5","C5"],
             ["A1","B1","C1"],
             ["A2","B2","C2"],
             ["A3","B3","C3"],
             ["A4","B4","C4"],
           ];

For example, I want to add ["C1", "C2", "C3"] or update the second line to ["Z1" , "Z2", "Z3"]

Abhi
  • 4,123
  • 6
  • 45
  • 77
  • For adding elements to array refer: [PUSH](https://www.w3schools.com/jsref/jsref_push.asp) or To add element to a specific posotion in array refer: [THIS](https://stackoverflow.com/a/586189/2968762) – Abhi Sep 18 '17 at 11:28
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – Ivan Sivak Sep 18 '17 at 11:29

2 Answers2

2

If you want to access the topmost array, you can push or popor a number of other native array methods. You can access the inner arrays by index (array[0] for example), and then use regular array methods on the inner arrays. You can read more about arrays and their methods here

To answer your two examples, to add a new array, you'd array.push(newArray) where newArray is the array you wanted to insert, ["C1", "C2", "C3"], if you wanted to update the second line, you could access it by index array[1] and set it equal to a new value, array[1] = ["Z1" , "Z2", "Z3"]

TheCog19
  • 1,129
  • 12
  • 26
  • How can I figure it out what the index of the array is when I just know a unique value ist for example Z1? –  Sep 18 '17 at 11:58
-1
array.push(["C1", "C2", "C3"] );

array[1] = ["Z1" , "Z2", "Z3"];
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
romph
  • 487
  • 3
  • 11