Suc*_*Man 3 javascript arrays object
我的脚本JavaScript是这样的:
<script>
var customer = {"name":"John", "address":'London'};
var products = [
{"product_name":"clothes", "quantity":1, "price":1000},
{"product_name":"trousers", "quantity":1, "price":500},
{"product_name":"shoes", "quantity":1, "price":2000}
];
</script>
Run Code Online (Sandbox Code Playgroud)
我想连接对象.所以我想要这样的结果:
我该怎么做?
您可以使用点语法和=运算符来设置customer对象的属性:
var customer = {"name":"John", "address":'London'};
var products = [
{"product_name":"clothes", "quantity":1, "price":1000},
{"product_name":"trousers", "quantity":1, "price":500},
{"product_name":"shoes", "quantity":1, "price":2000}
];
customer.products = products;
console.log(customer)Run Code Online (Sandbox Code Playgroud)
如果您不想customer更改,可以使用扩展语法:
var customer = {"name":"John", "address":'London'};
var products = [
{"product_name":"clothes", "quantity":1, "price":1000},
{"product_name":"trousers", "quantity":1, "price":500},
{"product_name":"shoes", "quantity":1, "price":2000}
];
const customerAndProducts = { ...customer, products : products }
console.log(customer)
console.log(customerAndProducts)Run Code Online (Sandbox Code Playgroud)