D
D
Dmitry Sokolov2019-11-09 22:18:59
Node.js
Dmitry Sokolov, 2019-11-09 22:18:59

How to remove a number from an array in JSON?

How to remove a number from an array in JSON?
[123, 125, 555]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Somewhere Intech, 2019-11-09
@sokolov_NaN

1. You need to accept the fact that this is also an array
2. You need to realize that node is still a "prefix" to js
3. Solution
3.1. delete method

var a = [123, 125, 555];
delete a[1];
console.log(a);
//[123, null, 555]

Those. null will remain (even if you do JSON.stringify). It most likely is not necessary
3.2. splice method
a.splice(1,1); // первое число это индекс, второе - количество удаляемых. Функция вернёт удаленные элементы, поэтому просто её вызываем
console.log(a);
// [123, 555]

Removed, after serialization ( JSON.stringify(a) ) it won't be present in the string either. But..
3.3. filter method - apply it also to the array a to prove that the element index is still left after the splice function
a = a.filter( (index, value) => index != 1 );
console.log(a);
// [123, 555]

Now, purely
PS, you can choose any suitable method, you don’t need to follow the sequence - I painted it for clarity of the result

S
Shohruh Shaimardonov, 2019-11-09
@joeberetta

Now, just first I'll take out my crystal ball and see how your JSON looks like with numbers...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question