-1

There are excellent posts about deleting elements and arrays from arrays, but I can't find any that does it for this situation:

How can I remove the array containing UUID as its first element?

let arr = [
  ['Log entry ID', '_id'],
  ['UUID', '_source.uuid'],
  ['Timestamp', '_source.@timestamp'],
  ['Description', '_source._desc']
];
Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162

4 Answers4

4

You can use Array.filter() and Array.includes() together as follows:

let arr = [
  ['Log entry ID', '_id'],
  ['UUID', '_source.uuid'],
  ['Timestamp', '_source.@timestamp'],
  ['Description', '_source._desc']
];

arr = arr.filter(subarr => !subarr.includes('UUID'));
// Or you can use subarr[0] !== 'UUID' if you want to check only first element

console.log(arr)
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
  • 1
    You can either use `!subarr[0] !== 'UUID'` to check the **first** element or `!subarr.includes('UUID')` to check for **any** element. `!subarr[0].includes('UUID')` does not make sense in this case. – Harun Yilmaz Nov 03 '21 at 13:27
2

If you only want to remove elements whose first item is 'UUID', you can set the condition of the filter to whether the first item is not 'UUID':

let arr = [
  ['Log entry ID', '_id'],
  ['UUID', '_source.uuid'],
  ['Timestamp', '_source.@timestamp'],
  ['Description', '_source._desc']
];

const res = arr.filter(e => e[0]!='UUID')

console.log(res)
Spectric
  • 30,714
  • 6
  • 20
  • 43
1

Checking the first element has been well covered by @RameshReddy and @HarunYilmaz.

Another way to check the first element would be:

arr = arr.filter(([first]) => first !== 'UUID');

let arr = [
  ['Log entry ID', '_id'],
  ['UUID', '_source.uuid'],
  ['Timestamp', '_source.@timestamp'],
  ['Description', '_source._desc']
];

arr = arr.filter(([first]) => first !== 'UUID');

console.log(arr);
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • 1
    Adding `...rest` as a second argument causes an unnecessary iteration-per-iteration each time the `filter()` iterates over the top-level array. Because you are not using the `...rest` values, you can simply omit them, destructuring only the first value into the arguments. – Brandon McConnell Nov 03 '21 at 16:57
  • @BrandonMcConnell, you're absolutely right. Thank you. – PeterKA Nov 04 '21 at 14:45
  • Glad I could help an otherwise great solution. The only other suggestion I'd give, which I called out in my post as well is to use `splice()` rather than `filter()` because `splice()` allows us to define our array variable with `const` rather than `let`, which we should always opt for when as have opportunity. – Brandon McConnell Nov 04 '21 at 22:15
  • 1
    I left the initial downvote on your answer for that reason, as well as my initial suggestion. I didn't mean any offense by it, and as your answer has improved, I've since removed the downvote. Cheers :) – Brandon McConnell Nov 04 '21 at 22:17
1

By default, you should be using const wherever possible, not let, so filter() is not an ideal solution.

Using const, you can still delete the desired array element effectively using the splice() method.

The splice() method follows the syntax splice(start, deleteCount, ...items) where start is the index to start the splice at, deleteCount is the number of elements you wish to remove starting at the start index, and items is any number of items you wish to inject at that same index once the deletion has completed.

Deletion is optional and can be subbed for 0 if you do not wish to delete any elements, but for this question, we will be removing 1 element, and we will exclude the items argument which is also optional, as we do not want to inject any additional elements.

For your task, we first need to find the index of the array element which has a first element equal to UUID. We can so using the findIndex() like this:

const index = arr.findIndex(e => e[0] === 'UUID');

Next, we can perform our splice() using that index value like this:

arr.splice(index, 1); // 1 for deleting a single array element

We an combine these two steps into one like this:

arr.splice(arr.findIndex(e => e[0] === 'UUID'), 1);

Here it is in action, including the array definition:

const arr = [
  ['Log entry ID', '_id'],
  ['UUID', '_source.uuid'],
  ['Timestamp', '_source.@timestamp'],
  ['Description', '_source._desc']
];

arr.splice(arr.findIndex(e => e[0] === 'UUID'), 1);

console.log(arr);
Brandon McConnell
  • 5,776
  • 1
  • 20
  • 36
  • Thanks a lot for the detailed explanation. Why doesn't `const` protect `splice()` from modifying it? Are there other functions that can modify `const`s? I have never heard of this before. – Sandra Schlichting Nov 04 '21 at 23:37
  • Yes! `const` variables cannot be simply "reset" to any other value. When we set the value of a `const` variable to an array, we cannot reset the variable by directly setting it to another value (e.g. `arr = [1]`), but because arrays are objects, we can mutate the array itself by performing actions on it. This is true for all objects— arrays, plain objects, String objects, Number objects, etc. This will not however work for primitives, which is what we are often accustomed to working with when we use strings and numbers: https://javascripttutorial.net/javascript-primitive-vs-reference-values – Brandon McConnell Nov 05 '21 at 01:14
  • @SandraSchlichting If this answered your question, would you mind selecting my solution as the accepted answer for your question? – Brandon McConnell Nov 05 '21 at 01:15