Reputation: 391
I have a nested JSON
which looks like this:
var unfilteredJSON = {
"payload":{
"oldKeys":[
"125262"
],
"keyData":[
{
"key":"123456",
"products":[
{
"prodId":"H1",
"qty":"1"
},
{
"prodId":"H2",
"qty":""
}
],
"rushFee":"true"
},
{
"key":"234234",
"products":[
{
"prodId":"H1",
"qty":"1"
},
{
"prodId":"H2",
"qty":""
}
],
"rushFee":"false"
}
],
"submit":"false"
}
}
The qty key can have empty values in the object. I want to filter the data with jQuery
method and remove the object
with blank qty so the JSON
can look like this -
{
"payload":{
"oldKeys":[
"125262"
],
"keyData":[
{
"key":"123456",
"products":[
{
"prodId":"H1",
"qty":"1"
},
],
"rushFee":"true"
},
{
"key":"234234",
"products":[
{
"prodId":"H1",
"qty":"1"
},
],
"rushFee":"false"
}
],
"submit":"false"
}
}
Please help.
Upvotes: 0
Views: 63
Reputation: 56975
Here's a filter
approach which mutates the object in-place:
var unfilteredJSON = {"payload":{"oldKeys":["125262"],"keyData":[{"key":"123456","products":[{"prodId":"H1","qty":"1"},{"prodId":"H2","qty":""}],"rushFee":"true"},{"key":"234234","products":[{"prodId":"H1","qty":"1"},{"prodId":"H2","qty":""}],"rushFee":"false"}],"submit":"false"}};
unfilteredJSON.payload.keyData.forEach(e =>
e.products = e.products.filter(p => p.qty)
);
console.log(unfilteredJSON);
Upvotes: 2