3

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:

<input 
 name="uppyResult"
 type="hidden"
 value="[{
  &quot;successful&quot;:[
   {&quot;id&quot;:&quot;uppy-a8geo/fw/400x400/jpg-1d-2v-1e-image/jpeg-10097-1626922525568&quot;},
   {&quot;id&quot;:&quot;uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772&quot;}
  ],
  &quot;failed&quot;:[],
  &quot;uploadID&quot;:&quot;ckss6e4xv00023h63uov94n5e&quot;
 }]"
>

I get the element with document.getElementsByName("uppyResult")[0].value; and then I parse it with const obj = JSON.parse(json).

How do I then remove only the index where id: uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772 and reinsert this as a string into the DOM?

Edit: previous version had " instead of &quot; inside value

calyxofheld
  • 1,538
  • 3
  • 24
  • 62

3 Answers3

2

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)
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
1

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);
Amir MB
  • 3,233
  • 2
  • 10
  • 16
1

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)
var filter_data = value => data.filter( ({successful}) => ! successful.find( ({id}) => id == value ))
var filter_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.

Tibrogargan
  • 4,508
  • 3
  • 19
  • 38