0

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
]
2

2 Answers 2

3

let arr = [
  {
    "addrSeq": "12",
    "addinfo": [
      {
        "virAddrSeq": "45"
      }
    ]
  },
  null
];
arr = arr.filter((i)=>i !== null && typeof i !== 'undefined');
console.log(arr);

Sign up to request clarification or add additional context in comments.

Comments

3

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.