0

const data=[{"NAME":"ABC","ID":"1","CATALOG":"PDC"},{"NAME":"DOT","ID":"88","CATALOG":"PDC"},{"NAME":"TIM","ID":"99","CATALOG":"PDC"},{"NAME":"TOM","ID":"19","CATALOG":"PDC"},{"NAME":"CAT","ID":"18"},{"NAME":"BAT","ID":"13"},{"NAME":"XYZ","ID":"12"},{"NAME":"PQR","ID":"11"}]
for(var i=0;i<data.length;i++)
{
    if (data[i].hasOwnProperty('CATALOG')) 
    {
    console.log(data[i].ID)
    }
    else
    {
        
        data.splice(i,1)
        //delete(data[i])
    }
}
console.log(data)

Above is my code but the result is not what is expected. The last 2 rows are being retained when using splice and using delete() creates undefined in place of the object without the property CATALOG.

samyuktha
  • 11
  • 3
  • You can use [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to filter out elements based on a given condition – Reyno Jun 16 '21 at 11:22
  • Does this answer your question? [Remove Object from Array using JavaScript](https://stackoverflow.com/questions/10024866/remove-object-from-array-using-javascript) – Reyno Jun 16 '21 at 11:25

2 Answers2

0

Here you go

Array.filter() docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Object.keys() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Array.some() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

const data=[{"NAME":"ABC","ID":"1","CATALOG":"PDC"},{"NAME":"DOT","ID":"88","CATALOG":"PDC"},{"NAME":"TIM","ID":"99","CATALOG":"PDC"},{"NAME":"TOM","ID":"19","CATALOG":"PDC"},{"NAME":"CAT","ID":"18"},{"NAME":"BAT","ID":"13"},{"NAME":"XYZ","ID":"12"},{"NAME":"PQR","ID":"11"}]
const newArray = data.filter(item => Object.keys(item).some(key => key === 'CATALOG'))
console.log(newArray)
Nikita Mazur
  • 1,602
  • 1
  • 5
  • 14
  • Please explain what you did otherwise its just a snippet. – Ajay Gupta Jun 16 '21 at 11:30
  • I don't have to explain obvious things, its as if I ask you to explain why you write to me "Please explain what you did otherwise its just a snippet." exactly using theese words and how you combined these words into sentence, why in this order. – Nikita Mazur Jun 16 '21 at 11:52
  • Yes you are right. But it might not be obvious to other people or the beginners who find this answer in the future. Having some documentation/explanation allows them to learn about what they are doing instead of just copy pasting. Thanks! – Ajay Gupta Jun 16 '21 at 12:56
0

You can use Array.filter method. It creates an array with only the entries that returns true in the callback function.

const data=[{"NAME":"ABC","ID":"1","CATALOG":"PDC"},{"NAME":"DOT","ID":"88","CATALOG":"PDC"},{"NAME":"TIM","ID":"99","CATALOG":"PDC"},{"NAME":"TOM","ID":"19","CATALOG":"PDC"},{"NAME":"CAT","ID":"18"},{"NAME":"BAT","ID":"13"},{"NAME":"XYZ","ID":"12"},{"NAME":"PQR","ID":"11"}]
const dataFiltered = data.filter((object)=> object.hasOwnProperty('CATALOG'))
console.log(dataFiltered)