I'm having trouble cycling through a multidimensional array and deleting a specific element array. My multidimensional array looks a little like this:
myArray = [["Tom", "161"], ["Dick", "29"], ["Harry", "46"]];
So if I have the number 29. What's the most efficient way to cycle through this array and delete the array element who's second element is 29? i.e. ["Dick", "29"]
asked Jul 11, 2013 at 19:44
4
var myArray = [["Tom", "161"], ["Dick", "29"], ["Harry", "46"]];
var myNewArray = myArray.filter[function[item]{ return item[1] != 29 }]
.filter uses native code to loop over your array. Building a new array could of course be more expensive than just cutting a part out of the old one, to be tested.
answered Jul 11, 2013 at 19:52
tomdemuyttomdemuyt
4,5221 gold badge31 silver badges59 bronze badges
2
myArray = [["Tom", "161"], ["Dick", "29"], ["Harry", "46"]];
for[var i = 0; i {
let percentage = [[activity[1] / 24] * 100].toFixed[];
activity[2] = percentage + '%';
}];
console.table[activities];
Code language: JavaScript [javascript]
The following shows the output in the console:
Code language: plaintext [plaintext]
┌─────────┬───────────────┬───┬───────┐ │ [index] │ 0 │ 1 │ 2 │ ├─────────┼───────────────┼───┼───────┤ │ 0 │ 'Work' │ 9 │ '38%' │ │ 1 │ 'Programming' │ 2 │ '8%' │ │ 2 │ 'Eat' │ 1 │ '4%' │ │ 3 │ 'Commute' │ 2 │ '8%' │ │ 4 │ 'Play Game' │ 1 │ '4%' │ │ 5 │ 'Sleep' │ 7 │ '29%' │ │ 6 │ 'Study' │ 2 │ '8%' │ └─────────┴───────────────┴───┴───────┘
Removing elements from the JavaScript multidimensional array
To remove an element from an
array, you use the pop[]
or splice[]
method.
For example, the following statement removes the last element of the activities
array.
Code language: CSS [css]
activities.pop[]; console.table[activities];
Output:
Code language: plaintext [plaintext]
┌─────────┬───────────────┬───┬───────┐ │ [index] │ 0 │ 1 │ 2 │ ├─────────┼───────────────┼───┼───────┤ │ 0 │ 'Work' │ 9 │ '38%' │ │ 1 │ 'Programming' │ 2 │ '8%' │ │ 2 │ 'Eat' │ 1 │ '4%' │ │ 3 │ 'Commute' │ 2 │ '8%' │ │ 4 │ 'Play Game' │ 1 │ '4%' │ │ 5 │ 'Sleep' │ 7 │ '29%' │ └─────────┴───────────────┴───┴───────┘
Similarly, you can remove the elements from the inner array of the multidimensional array by using the pop[]
method. The following example removes the percentage element from the inner arrays of the activities
array.
Code language: JavaScript [javascript]
activities.forEach[[activity] => { activity.pop[2]; }]; console.table[activities];
Output:
Code language: plaintext [plaintext]
┌─────────┬───────────────┬───┐ │ [index] │ 0 │ 1 │ ├─────────┼───────────────┼───┤ │ 0 │ 'Work' │ 9 │ │ 1 │ 'Programming' │ 2 │ │ 2 │ 'Eat' │ 1 │ │ 3 │ 'Commute' │ 2 │ │ 4 │ 'Play Game' │ 1 │ │ 5 │ 'Sleep' │ 7 │ └─────────┴───────────────┴───┘
Iterating over elements of the JavaScript multidimensional array
To iterate a multidimensional array, you use a nested for loop as in the following example.
// loop the outer array for [let i = 0; i < activities.length; i++] { // get the size of the inner array var innerArrayLength = activities[i].length; // loop the inner array for [let j = 0; j < innerArrayLength; j++] { console.log['[' + i + ',' + j + '] = ' + activities[i][j]]; } }
Code language: JavaScript [javascript]
The first loop iterates over the elements of the outer array and the nested loop iterates over elements of the inner array.
The following shows the output of the script in the console:
[0,0] = Work [0,1] = 9 [1,0] = Eat [1,1] = 1 [2,0] = Commute [2,1] = 2 [3,0] = Play Game [3,1] = 1 [4,0] = Sleep [4,1] = 7 [5,0] = Study [5,1] = 2
Or you can use the forEach[]
method twice:
Code language: JavaScript [javascript]
activities.forEach[[activity] => { activity.forEach[[data] => { console.log[data]; }]; }];
Output:
Code language: plaintext [plaintext]
Work 9 Eat 1 Commute 2 Play Game 1 Sleep 7 Study 2
In this tutorial, you have learned how to use an array of arrays to create a JavaScript multidimensional array.
Was this tutorial helpful ?