2 回答
TA贡献1993条经验 获得超5个赞
您可以编写一个函数并获得所需的属性:
var obj1 ={
start: "16:01", end: "23:59", totalqty: 1065, totalamount: 8229170
}
var obj2 = {
qty: 10,
amt: 120
}
const merge = (obj1, obj2) => {
return {
time: obj1.start + '-' + obj1.end,
val: obj2.qty,
totalqty: obj1.totalqty,
totalamount: obj1.totalamount,
price: obj2.amt
};
}
console.log(merge(obj1, obj2));
此外,您可以使用spread语法,但它会合并所有属性:
let obj1 ={
start: "16:01", end: "23:59", totalqty: 1065, totalamount: 8229170
}
let obj2 = {
qty: 10,
amt: 120
}
let merged = {...obj1, ...obj2};
console.log(merged );
或尝试使用Object.assign()它也合并了所有属性:
var obj1 ={
start: "16:01", end: "23:59", totalqty: 1065, totalamount: 8229170
}
var obj2 = {
qty: 10,
amt: 120
}
var result = Object.assign({}, obj1, obj2);
console.log(result);
TA贡献1966条经验 获得超4个赞
您可以解构对象,重命名一些属性并返回一个新对象。
const
merge = ({ start, end, totalqty, totalamount }, { qty: val, amt: price }) =>
({ time: start + "-" + end, val, totalqty, totalamount, price}),
obj1 = { start: "16:01", end: "23:59", totalqty: 1065, totalamount: 8229170 },
obj2 = { qty: 10, amt: 120 };
console.log(merge(obj1, obj2));
添加回答
举报