3

我得到了这个 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).

然后如何删除索引 whereid: uppy-maxresdefault/jpg-1e-image/jpeg-81700-1626406845772并将其作为字符串重新插入到 DOM 中?

编辑:以前的版本有"而不是&quot;里面value

4

3 回答 3

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

于 2021-08-26T02:37:45.357 回答
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);

于 2021-08-26T02:16:23.547 回答
1

你不是特别清楚你想删除什么,但这里有两种可能性:

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

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

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

如果您想从作为数组的“值”的任何成员中删除与搜索词匹配的任何对象,那么@Amir MB 的技术是优越的 - 这样.reduce()做并且还创建副本,避免突变。同样,不清楚这是否是一项要求。

于 2021-08-26T02:59:57.943 回答