求和与对象属性值插入

2020-10-16  本文已影响0人  理子

js求和

    let data = [{
        "goods_num": 3,
        "goods_price": 1,

    }, {
        "goods_num": 10,
        "goods_price": 1,


    }, {
        "goods_num": 4,
        "goods_price": 1,

    }];

    let price = data.reduce((p, e) => p + e.goods_price * e.goods_num, 0);

    console.log(price)

新数组:array;
原数组:list;
给原数组list的对象加一个名为aaaaa的属性,属性值为'11111'
然后打印 console.log(array); 即可看到需要的值

    let list = [
        {a:1},
        {b:1},
        {c:1}
    ];
    let array=[];
    list.map((item,index)=>{
        array.push(
            Object.assign({},item,{bbc:'11111'})
        )
    })
    console.log(array);  
上一篇 下一篇

猜你喜欢

热点阅读