js implementation deletes unwanted fields and values in Json

Problem scenario: In a recent project, there is a function that needs to export Excel, and the fields displayed on the download page need to be downloaded. Since the API is connected to three similar modules, the returned value is more than what I want, and the API is not written as a Filter. format, the front-end cannot be spliced ​​through parameters (the back-end is based on LoopBack), so the front-end realizes the filtering of corresponding fields

Solution: Use delete to delete unwanted fields and values

Code example:

 const data=[
{
    
    
  name:"Steven",
  age:"21",
  address:"上海",
  sex:"男", 
  },
  {
    
    
  name:"Jack",
  age:"5",
  address:"苏州",
  sex:"男", 
  },
  {
    
    
  name:"Allan",
  age:"23",
  address:"北京",
  sex:"女", 
  }
  ]
  //现在想要删除 address sex 字段和对应的值,实现方法如下
  const deleteData=data.map(item=>{
    
    
  delete item.address
  delete item.sex
  return item
  })
  console.log(deleteData)  //打印后得到自己想要的结果

The delete method is very convenient and simple, so record it~~

Guess you like

Origin blog.csdn.net/weixin_45680024/article/details/122294394