js 从一个数组对象中取key 和value组成一个新的对象

67 篇文章 1 订阅

从一个数组对象中取key 和value组成一个新的对象

const type = [
                    { amount: '', size: '4013' },
                    { amount: '1', size: '1613' },
                    { amount: '5', size: '2007' },
                    { amount: '9', size: '3613' },
                    { amount: '', size: '2413' },
                    { amount: '10', size: '1213' },
                    { amount: '', size: '3213' },
                    { amount: '11', size: '2013' },
                    { amount: '', size: '0807' },
                    { amount: '', size: '2807' },
                    { amount: '6', size: '1607' }
             ]
 let newArr={};
    type .map(itm=>{
            newArr[itm.size] = itm.amount
      })
console.log('1',newArr)
{1213: '10', 1607: '6', 1613: '1', 2007: '5', 2013: '11', 2413: '', 2807: '', 3213: '', 3613: '9', 4013: '', 0807: ''}

tips:去掉里面的空值

 let newArr={};
    type .map(itm=>{
        if (itm.amount) {
             newArr[itm.size] = itm.amount
          }
      })
console.log('1',newArr)
{1213: '10', 1607: '6', 1613: '1', 2007: '5', 2013: '11', 3613: '9'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值