Answer the question
In order to leave comments, you need to log in
How to remove null from JSON?
Null appears in json when deleting or adding new data.
It looks like this:
[{"test":-212}, null, {"test":-121}]
Delete via splice()
Add new data via push()
Answer the question
In order to leave comments, you need to log in
Found a way.
//Подготовим массив для тестирования
var myArray = [1111, 5, 3, 4, null, undefined, 4, 5, null, 5];
//Удаляем все элементы, равные undefined и null
myArray = myArray.filter(function(x) {
return x !== undefined && x !== null;
});
console.log(myArray.toString()); //1111,5,3,4,4,5,5
You can use filter. Then a new array will be created without the old element, which can be dumped into JSON without garbage.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question