json数组对某个键进行值修改,json数组去除某个键和值

前端 2019-11-24T17:30:25 浏览:72

有的时候需要对json数组进行简化或者修改某个键的值,这时我门可以通过遍历json数组对值进行修改或者对键和值进行删除。

1.原始的数组 arrayMla

[{
  "id": "",
  "imgId": 8,
  "src": "20191124/55b7c67f-b3ad-4617-9d8b-9ed7dbad2941.jpg",
  "display": "http://localhost:5000/assets/20191124/55b7c67f-b3ad-4617-9d8b-9ed7dbad2941.jpg",
  "fileSize": 110169,
  "fileName": "核验单.jpg",
  "fileType": "image/jpeg",
  "isAnimated": null
}, {
  "id": "",
  "imgId": 7,
  "src": "20191124/3fb8b8a4-034a-451b-9c0f-c78c45bff5bd.jpg",
  "display": "http://localhost:5000/assets/20191124/3fb8b8a4-034a-451b-9c0f-c78c45bff5bd.jpg",
  "fileSize": 88255,
  "fileName": "域名证书.jpg",
  "fileType": "image/jpeg",
  "isAnimated": null
}]

2.我想要的数组是这样的

[{
  "id": "0",
  "imgId": 8,
  "src": "20191124/55b7c67f-b3ad-4617-9d8b-9ed7dbad2941.jpg",
  "display": "http://localhost:5000/assets/20191124/55b7c67f-b3ad-4617-9d8b-9ed7dbad2941.jpg"
}, {
  "id": "1",
  "imgId": 7,
  "src": "20191124/3fb8b8a4-034a-451b-9c0f-c78c45bff5bd.jpg",
  "display": "http://localhost:5000/assets/20191124/3fb8b8a4-034a-451b-9c0f-c78c45bff5bd.jpg"
}]

怎么实现呢!

arrayMla.forEach((item, index) => {
  const me = item
  item.id = index
  delete item.fileSize
  delete item.fileName
  delete item.fileType
  delete item.isAnimated
})

通过forEach 可以对json数组进行再编辑返回新的json数组