修改对象数组中的值

时间:2019-04-14 18:50:29

标签: javascript arrays object javascript-objects

假设有一个对象数组:

package main

import (
    "fmt"
    "log"
)

type foo func(string, string)

func bar(route string, callback foo) bool {
 //...logic

  /* you can return the callback to use the 
     parameters and also in the same way you 
     could verify the bar function
   */
    callback("param", "param")
    return true
}

func main() {
    fmt.Println("Hello, playground")

    res := bar("Hello", func(arg1, arg2 string) {
        log.Println(arg1 + "_1")
        log.Println(arg2 + "_2")
    })

    fmt.Println("bar func res: ", res)
}

我想计算值let A = [ { id: 1, item: 'item 1', qty: 23, unitPrice: 10; totalAmount: qty*price, }, { id: 2, item: 'item 2', qty: 3, unitPrice: 30, totalAmount: qty*price, } ]; 。我要如何针对整个列表?

1 个答案:

答案 0 :(得分:1)

您可以使用map()并返回具有所有先前属性和新属性totalAmount的新对象

let arr = [ { id: 1, item: 'item 1', qty: 23, unitPrice: 10 }, { id: 2, item: 'item 2', qty: 3, unitPrice: 30, } ];

let res = arr.map(x => ({...x, totalAmout:x.unitPrice * x.qty}));

console.log(res)