一个对象怎么删除等于0的key值

let arr = [{allotQuantity:0,a:1},{allotQuantity:1,a:2}]

如果allotQuantity等于0就删除这条数组对象,该怎么写?

阅读 2.6k
3 个回答
arr.map(i => {
    i.allotQuantity == 0 ? delete i.allotQuantity : null
    return i
})

??? 到底删对象还是对象的key?

let newArr = arr.filter((item,key) => {
   return item.allotQuantity != 0
})
console.log(newArr)
const arr = [{ allotQuantity: 0, a: 1 }, { allotQuantity: 1, a: 2 }];
const result = arr.filter(item => item.allotQuantity !== 0);
// [
//   {
//     "allotQuantity": 1,
//     "a": 2
//   }
// ]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题