删除对象数组内不需要的字段生成新的数组

去除一个对象数组里不需要的键值对 生成新的数组

<script>
    let a=[
        {
          createTime: "2020-06-19 14:48:57",
          delFlag: 1,
          goodsCategory: "1273869332852719616",
          goodsCode: "123456",
          goodsName: "感冒灵颗粒",
          goodsPic: "1273870309353799680",
          goodsPrice: 9,
          id: "1273870363699396608",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 13,
          sort: 1,
          source: 1,
          stockNum: 99986,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-25 12:26:49",
        },
        {
          createTime: "2020-06-19 14:55:25",
          delFlag: 1,
          goodsCategory: "1273869379375939584",
          goodsCode: "5",
          goodsName: "阿莫西林胶囊",
          goodsPic: "1273871948731072512",
          goodsPrice: 7,
          id: "1273871990716055552",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 2,
          sort: 1,
          source: 1,
          stockNum: 99997,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-21 17:47:27",
        },
        {
          createTime: "2020-06-19 14:55:57",
          delFlag: 1,
          goodsCategory: "1273869379375939584",
          goodsCode: "4",
          goodsName: "蒲地蓝消炎片",
          goodsPic: "1273872070277808128",
          goodsPrice: 5,
          id: "1273872125143498752",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 1,
          sort: 1,
          source: 1,
          stockNum: 99998,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-25 10:33:42",
        },

      ];

    var newObj = {};
    var newArr = [];
    a.forEach(function(item,i){
        for ( var key in item) {
            if(key != 'goodsName'&& key != 'salesNum'){
                newObj[key] = item[key];
            }
        }
        newArr.push(newObj);
        newObj = {};//这步至关重要,每循环一次,都要清空一次,否则拿到的数据总是最后一条
    });  
    console.log(newArr);
</script>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值