代码之家  ›  专栏  ›  技术社区  ›  calyxofheld

如何在json数组中搜索值,然后在找到值时删除索引

  •  2
  • calyxofheld  · 技术社区  · 4 年前

    我得到了这个json字符串,我需要从中解析和删除数据,但我不确定如何处理它。假设我有以下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;
     }]"
    >
    

    我得到的元素与 document.getElementsByName("uppyResult")[0].value; 然后我用 const obj = JSON.parse(json)

    然后我如何删除 只有 索引在哪里 id: uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772 然后将其作为字符串重新插入DOM?

    编辑:以前的版本有 " 而不是 &quot; 在…内 value

    2 回复  |  直到 4 年前
        1
  •  2
  •   Yosvel Quintero    4 年前

    你可以做:

    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)
        2
  •  1
  •   Amir MB    4 年前

    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);
        3
  •  1
  •   Tibrogargan    4 年前

    您对要删除的内容不是特别清楚,但以下是两种可能性:

    • 第一个过滤器使用分解并查找id匹配的任何元素。
    • 第二个筛选器将删除成功数组中与搜索id匹配的所有条目。请注意,注意不要更改原始数据-复制数组中的每个对象并更改它们(不确定这是否重要)。

    为测试目的向值数组添加了一个额外的元素。

    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'))

    .reduce() 做到这一点,并创建副本,避免变异。同样,不清楚这是否是一项要求。

    推荐文章