Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a column from a multidimensional array in javascript

Tags:

I have a 2D array:

var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]]

I want to delete an entire column of this array (i.e. delete every third element within each array).

There are solutions here and here, but neither of them is in javascript, so I'm having trouble relating the situations.

What's the best way to approach this problem? I don't want to use .splice() because in some cases I'll be deleting multiple columns, and the .splice() method will change the length of the array, so I end up accessing out of bounds.

Thanks!

like image 391
user3757174 Avatar asked Jul 17 '14 19:07

user3757174


People also ask

How can I remove a specific item from an array in JavaScript?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do you delete the third element of an array in JavaScript?

Answer: Use the splice() Method You can use the splice() method to remove the item from an array at specific index in JavaScript. The syntax for removing array elements can be given with splice(startIndex, deleteCount) .

How do you remove indices from an array?

Find the index of the array element you want to remove using indexOf , and then remove that index with splice . The splice() method changes the contents of an array by removing existing elements and/or adding new elements. The second parameter of splice is the number of elements to remove.


4 Answers

Try using slice. It won't alter any changes to your original array

var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]]  var x = array.map(function(val) {   return val.slice(0, -1); });  console.log(x); // [[a,b],[a,b],[a,b]]
like image 159
Rahil Wazir Avatar answered Oct 29 '22 04:10

Rahil Wazir


This function doesn't use splice and it removes any column you want:

function removeEl(array, remIdx) {  return array.map(function(arr) {          return arr.filter(function(el,idx){return idx !== remIdx});           }); }; 

Hope this is what you are looking for

like image 21
sergeyz Avatar answered Oct 29 '22 04:10

sergeyz


Iterate through the array and splice each sub array:

var idx = 2;
for(var i = 0 ; i < array.length ; i++)
{
   array[i].splice(idx,1);
}

JSFiddle.

Edit: I see you don't want to use splice due to out-of-bounds problems and array length changing. So:

1.You can check if you're out of bounds and skip the slice.
2.You can create an array of indexes you want to delete and simply create new arrays from the indexes that don't appear in that array (instead of deleting, create new arrays with the opposite condition).

Something like this:

var array = [
    ["a", "b", "c"],
    ["a", "b", "c"],
    ["a", "b", "c"]
];

var idxToDelete = [0,2];

for (var i = 0; i < array.length; i++) {
    var temp = array[i];
    array[i] = [];
    for(var j = 0 ; j < temp.length ; j++){
        if(idxToDelete.indexOf(j) == -1) // dont delete
        {
            array[i].push(temp[j]);
        }
    }
}

New JSFiddle.

like image 43
Amir Popovich Avatar answered Oct 29 '22 04:10

Amir Popovich


use the map function and splice function for your solution. like this

var array = [["a", "b", "c"],["a", "b", "c"],["a", "b", "c"]];

array = array.map(function(item){
        // the 0,2 tells the splice function to remove (skip) the last item in this array
        return item.splice(0,2);
});

console.log(array);
// [["a", "b"],["a", "b"],["a", "b",]];

don't use delete to delete items from a javascript array. the reason is that delete will toss the item but don't update the internal length variable.

example

var array = ["a", "b", "c"];

delete array[3];
console.log(array);
// ["a", "b"]
console.log(array.length);
// 3

array = array.splice(0,2);
console.log(array);
// ["a", "b"]
console.log(array.length);
// 2

use splice this sets the correct length and delete the item of the array.

like image 39
ins0 Avatar answered Oct 29 '22 02:10

ins0