How To Search Json Array For Value And Then Erase The Index If Value Is Found
I got this json string that I need to parse and remove data from, but I'm unsure of how to go about it. Say I have the following json:
Solution 1:
You can do:
const data = [{
"successful":[
{"id":"uppy-a8geo/fw/400x400/jpg-1d-2v-1e-image/jpeg-10097-1626922525568"},
{"id":"uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772"}
],
"failed":[],
"uploadID":"ckss6e4xv00023h63uov94n5e"
}]
const idToRemove = "uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772"const result = data.map(obj =>Object.entries(obj).reduce((a, [k, v]) => {
a[k] = Array.isArray(v)
? v.filter(item => item.id !== idToRemove)
: v
return a
}, {}))
console.log(result)
Solution 2:
const data = [{
"successful":[
{"id":"uppy-a8geo/fw/400x400/jpg-1d-2v-1e-image/jpeg-10097-1626922525568"},
{"id":"uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772"}
],
"failed":[],
"uploadID":"ckss6e4xv00023h63uov94n5e"
}];
const toRemove = "uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772";
data.forEach(item => {
Object.values(item).forEach(array => {
if (!Array.isArray(array))
return;
const index = array.findIndex(elm => elm.id === toRemove);
if (index > -1)
array.splice(index, 1);
});
});
console.log(data);
Solution 3:
You weren't particularly clear about what you wanted to remove, but here's both possibilities:
- First filter uses decomposition and finds any element where the id matches.
- Second filter removes all entries from successful arrays that match the search id. Note that care is taken not to mutate the original data - copied each object in the array and altered them (not sure if that was important).
Added an extra element to the value array for testing purposes.
let value='[{ \
"successful":[ \
{"id":"uppy-a8geo/fw/400x400/jpg-1d-2v-1e-image/jpeg-10097-1626922525568"}, \
{"id":"uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772"} \
], \
"failed":[], \
"uploadID":"ckss6e4xv00023h63uov94n5e" \
},{ \
"successful":[ \
{"id":"uppy-a8geo/fw/400x400/jpg-1d-2v-1e-image/jpeg-10097-1626922525568"}, \
{"id":"not-uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772"} \
], \
"failed":[], \
"uploadID":"some_other_id" \
}]'let data = JSON.parse(value)
varfilter_data = value => data.filter( ({successful}) => ! successful.find( ({id}) => id == value ))
varfilter_successful = value => data.map( elem => {
elem = Object.assign( {}, elem ) // copy element to avoid mutation
elem.successful = elem.successful.filter( ({id}) => id != value )
return elem
})
console.log('Remove any object from the values array where the search id is in the successful array')
console.log(filter_data('uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772'))
console.log('Remove successful entries that match the search id from all values')
console.log(filter_successful('uppy-a8geo/fw/400x400/jpg-1d-2v-1e-image/jpeg-10097-1626922525568'))
If you wanted to remove any object that matched the search term from any member of "value" that was an array then @Amir MB's technique is superior - the .reduce()
does that and also creates copies, avoiding mutation. Again, not clear if that was a requirement.
Post a Comment for "How To Search Json Array For Value And Then Erase The Index If Value Is Found"