0

I have a nested array of objects. Something like:

[
  {
    item: 'x',
    data: [
      item: 'y',
      data: [
        item: 'z',
        data: [...]
      ]
    ],
    ...
  }
]

I know the item I want to remove is located at position [1, 3, 1, 4]. Is there a faster way to remove that item in the array?

8
  • 1
    Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. You should know better with almost 10K rep Commented Mar 24, 2020 at 14:27
  • could you log the entire array please and identify which item you want to remove Commented Mar 24, 2020 at 14:28
  • 1
    I don't think that is valid JS syntax? Commented Mar 24, 2020 at 14:29
  • @mplungjan Please be more specific with your issue with my question and I'm happy to address it. I have searched SO and the web but didn't come up with a reasonable answer to my question. I'm always happy adjust my question given direct and constructive feedback. Commented Mar 24, 2020 at 14:29
  • 1
    Please post a real sample of the data. Your pseudocode is ambiguous. Commented Mar 24, 2020 at 14:34

1 Answer 1

2

Here is an example of how I would do it. I'm retrieving a pointer to the very array to mutate, then I mutate it.

What this code doesn't do is :

  • Doesn't handle the case the given value is incorrect (bad indexes)
  • The keys doesn't exists

What does this code do :

  • Handle the case we remove the first item or a deep item
  • Handle X nested levels
  • mutate the given object

const arr = [{
  item: 'x',
  data: [{
      item: 'y',
      data: [{
        item: 'z',
        data: [],
      }],
    },
    {
      item: 'y2',
      data: [{
        item: 'z',
        data: [{
          item: 'a',
          data: [],
        }, {
          item: 'b',
          data: [],
        }, {
          item: 'toRemove',
          data: [],
        }],
      }],
    }, {
      item: 'y3',
      data: [{
        item: 'z',
        data: [],
      }],
    },
  ],
}];

// /!\ Mutate the given object
function removeAtPos(obj, pos) {
  // Because we look for a pointer, we dont go all in
  // or we would mutate a copy of the data and not the given object data
  const allExceptLast = pos.slice(0, pos.length - 1);

  // ternary is here to handle the case if we have the first item or not
  // First item doesn't start with 'data'
  const ptr = allExceptLast.reduce((tmp, x) => tmp.data ?
                                               tmp.data[x] :
                                               tmp[x], obj);
  
  // Finally remove the wanted part of the array
  (ptr.data || ptr).splice(pos[pos.length - 1], 1);
}

removeAtPos(arr, [0, 1, 0, 2]);

console.log(arr);

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

2 Comments

In all of my many years of dev I've never seen the if/else short hand used with a comma a ? b : c, d like you did in the reduce. Is that a feature of the shorthand or reduce? Where can I read more about that?
there is the reduce reduce(func, starting_value); and then the ternary inside of the function. There is nothing special it. The following is the same : allExceptLast.reduce((tmp, x) => (tmp.data ? tmp.data[x] : tmp[x]), obj). It's a combo between, Array.reduce, ternary and arrow function single line syntax

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.