0

I have a 2-dimensional data structure like this:

    var arr = [
        {
            'subarr': [
                { 'a': '11' },
                { 'a': '12' }
            ]
        },
        {
            'subarr': [
                { 'a': '21' },
                { 'a': '22' }
            ]
        },
        {
            'subarr': [
                { 'a': '31' },
                { 'a': '32' }
            ]
        }
    ];

I need to insert an element, e.g. '23', or remove, e.g. '22'.

The slice method does not copy the entire data, but inserts or removes in the array 'in place'. But how do I apply that on a 2-dim array? This code will not work, because the [i] operation creates a new variable, so arr will not be changed at all:

arr[1].subarr.slice(0, 1);

I would need some reference type of variable to point at the subarray to be sliced.

Any help would be appreciated


As happens more often: the complicated problem has a simple answer. I should have used splice(), not slice(), as the answers told me.

2 Answers 2

2

To add push(), [], splice() or unshift()

arr[1].subarr.push({'a':'23'}); //add at the end

or

arr[1].subarr.unshift({'a':'23'}); //add in the beginig

to add at specific position (2th in this case)

arr[1].subarr.splice(2, 0, {'a':'433'}, {'a':'544'});

to remove splice

arr[1].subarr.splice(1,1); //splice(start,deleteCount)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but your solutions will return only the 2nd arr element, with the modified data. The variable 'arr' will not be changed.
Anything that starts with arr[i] will obviously not work. I need a solution for the problem of changing the variable arr.
@Roland arr[1].subarr.slice(0, 1); modifies arr
splice will actually modify your array not slice;
@Hikmat yes it's arr[1].subarr.splice(0, 1); I made a mistake in the comment
|
1

How to insert an element ie '23' without mutation:

    var arr = [
        {
            'subarr': [
                { 'a': '11' },
                { 'a': '12' }
            ]
        },
        {
            'subarr': [
                { 'a': '21' },
                { 'a': '22' }
            ]
        },
        {
            'subarr': [
                { 'a': '31' },
                { 'a': '32' }
            ]
        }
    ];
    
    var newArr = [...arr, arr[0].subarr.push({'a':'23'})]
    console.log(newArr)

How to remove an element ie '22' without mutation:

    var arr = [
        {
            'subarr': [
                { 'a': '11' },
                { 'a': '12' }
            ]
        },
        {
            'subarr': [
                { 'a': '21' },
                { 'a': '22' }
            ]
        },
        {
            'subarr': [
                { 'a': '31' },
                { 'a': '32' }
            ]
        }
    ];

var newArr = [...arr.map(element => element.subarr.filter(item => item.a !== '22'))]

console.log(newArr)

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.