0

I am trying to delete/remove an object which is at at position x in array A such that Array A is inside another array such as this:

    var lol = [ 
            [
              {'a':1},
              {'b':2},
              {'c':3}
            ], 
            [
              {'d':4},
              {'e':5},
              {'f':6}
            ], 
            [
              {'g':7},
              {'h':8},
              {'i':9}
            ]
];

Now lets say I would like to remove object with key 'f'. I have a

function deleteNestedElement(outerIndex, innerIndex) {
        lol[outerIndex].splice(0, innerIndex);
}

on calling

deleteNestedElement(2,0);
alert(lol);

I am getting the result as this:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Here is my JsFiddle

I need to delete the specific element as desired from this array.

4
  • 1
    you get the same type of output if you alert(lol) before your function call. you're alerting objects, not strings, so that's what your output is going to be. Try doing a console.log instead, or using the browser debugging tools. Commented Dec 20, 2018 at 13:50
  • 1
    You need to read up on how splice() works. Also using alert() is a useless debugging tool for arrays of objects Commented Dec 20, 2018 at 13:50
  • @charlietfl, thanks, I was wondering if splice is doing something to array that is why it is like this in alert. I get it now. Commented Dec 20, 2018 at 14:03
  • 1
    How would you know when you can't see what's in each object? Logging to console lets you see changes Commented Dec 20, 2018 at 14:04

2 Answers 2

2

To remove items Array.splice() expects an index (1st param), and the number of items you wish to remove (2nd param). You passed 0 and 0 - remove 0 items from index 0, so nothing was removed.

In addition alert() converts JS objects to strings, and this is what you see. Use console.log() instead.

function deleteNestedElement(outerIndex, innerIndex) {
  lol[outerIndex].splice(innerIndex, 1);
}

var lol = [[{"a":1},{"b":2},{"c":3}],[{"d":4},{"e":5},{"f":6}],[{"g":7},{"h":8},{"i":9}]];

deleteNestedElement(1, 2); // this will remove the "f" object
deleteNestedElement(2, 0); // this will remove the "g" object
console.log(lol);

Sign up to request clarification or add additional context in comments.

Comments

2

Array.splice function takes first parameter as index and second parameter as number of elements to be deleted. In our case it should be splice(innerIndex, 1) as we need to delete 1 element at a particular index i.e. innerIndex.

Also, index of array start from 0, hence for 2nd item in array, you will need to pass 1 for outerIndex and similarly 2 for innerIndex like deleteNestedElement(1,2)

var lol = [[{"a":1},{"b":2},{"c":3}],[{"d":4},{"e":5},{"f":6}],[{"g":7},{"h":8},{"i":9}]];

function deleteNestedElement(outerIndex, innerIndex) {
        lol[outerIndex].splice(innerIndex, 1);
}

deleteNestedElement(1,2);
console.log(lol);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.