The below format is fetching from server and its strangely adding null value in the last object. I want to remove null or undefined objecs from the array. Can anyone help me?
[
{
"addrSeq": "12",
"addinfo": [
{
"virAddrSeq": "45"
}
]
},
null
]
You can use Array.filter to filter out null and undefined items by checking whether the item is not null (an undefined check is unnecessary, since null == undefined).
const arr=[{addrSeq:"12",addinfo:[{virAddrSeq:"45"}]},null,undefined];
const result = arr.filter(e => e != null)
console.log(result)
let arr = [
{
"addrSeq": "12",
"addinfo": [
{
"virAddrSeq": "45"
}
]
},
null
];
arr = arr.filter((i)=>i !== null && typeof i !== 'undefined');
console.log(arr);