Reputation: 99
This is the input json
set = {
"pending": [
{
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86l89u",
}]}
set = set.pending[0].filter(({ po_id }) => {
return po_id === 'm86lu';
});
I need to get only json set that has po_id 'm86lu'.
The output needs to be like this
set = {
"pending": [
{
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}
]}
How do I get it? Looks like I am using the filter
function incorrectly.
Upvotes: 2
Views: 113
Reputation: 1411
Loop through json, add to new set one by one based on the suggestion from comment.
var input = {
"pending": [
{
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86lu",
}, {
"is_active": true,
"order_updated": false,
"po_id": "m86l89u",
}]}
var output = {};
for (var key in input) {
if (!input.hasOwnProperty(key)) {
continue;
}
let content = input[key].filter(({ po_id }) => { return po_id === 'm86lu'; });
output[key] = content;
}
console.log(output);
Upvotes: 0
Reputation: 2440
The pending key would be lost in that way
set = {
pending: set.pending.filter(({ po_id }) => { return po_id === 'm86lu'; })
}
Upvotes: 2