Reputation: 85
I have an array that contains plenty of data. The format is always like that:
1:
UserName: "John Smith"
Priority: "2"
Time Occured: "02/09/2019 11:20:23"
Time Ended: "02/09/2019 11:20:23"
2:
UserName: "Tom Bill"
Priority: "4"
Time Occured: "01/08/2019 13:20:23"
Time Ended: "04/08/2019 15:20:23"
3:
UserName: "John Smith"
Priority: "2"
Time Occured: "06/08/2019 13:20:23"
Time Ended: "09/09/2019 15:20:23"
...
Then I am changing this one into json file but it always starts with:
[
null,
{
I am using JSON.stringify to make it work
result = JSON.stringify(array);
I have tried a few options, yet unfortunately it always ends with null at the beginning. Is there any way to remove the null from the beginning of the json, or maybe to replace it?
Upvotes: 1
Views: 437
Reputation: 11
We can also use this method to remove null, undefined, NaN, empty string from array use this code. This will filter the array. After filtering you can Stringify it:
Array=[null,1,2,3,null,4,undefined,7,NaN,"",8,]
// this removes the null,undefined,NaN,"" from this Array
var filtered = Array.filter(function(el) { return el; });
JSON.stringify(filtered)
console.log(filtered) //"[1, 2, 3, 4, 7, 8]"
Upvotes: 1
Reputation: 3434
My advice would be to check why null
is always the first item, but if you really can't change that, then you can use .shift()
to remove the first element of the array before you stringify it:
const array = [null, 1, 2, 3, 4]
// this removes the first element
array.shift()
JSON.stringify(array)
>>> "[1, 2, 3, 4]"
Upvotes: 2