E
E
eshran2021-02-23 23:22:29
Node.js
eshran, 2021-02-23 23:22:29

How to remove elements from an array so that it is not replaced by null?

Hello, removing some element from the array, this element turns into null for me. How can this be fixed?

delete message.user

// -> 

[{ ..., null, ... }]


due to the fact that some element becomes null, the users.find() method stops working.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2021-02-24
@eshran

const arr = [1, 2, 3];
let index = 1; // берем индекс двойки. 
let count = 1; // сколько вырезать элементов

arr.splice( index, count );

console.log( arr ); // [1, 3]

R
rPman, 2021-02-23
@rPman

Do not use an array as an object (convert using Object.assign) or Map , in which case the sequence may be broken, and the order of elements by key may not be in order

M
mitya_k, 2021-02-24
@mitya_k

let res = [ 1, 2, 3];
res.forEach((el, i) => {
  if (el === 2) {
    delete res[i];
  }
})
res = res.filter(Boolean);
console.log(res); // [1, 3]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question