假设有一个对象数组:
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,
}
];
。我要如何针对整个列表?
答案 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)