I am trying to filter javascript object using filter function .But I am getting error key is not defined .here is my code https://jsfiddle.net/13n8n3om/
var arr=[
{
"EX": {
"division": "abc",
"is_active": true,
}
},
{
"PY": {
"division": "pqwww",
"is_active": false,
}
}
];
arr = arr.filter(function(obj) {
return obj[key] !== 'EX';
});
console.log(arr)
Expected output
[
{
"PY": {
"division": "pqwww",
"is_active": false,
}
}
]
keyobj[Object.keys(obj)[0]]would probably work.obj.hasOwnProperty('EX')should work. @Andy it may not work ifobjhas more than one properties.