我想知道如何使用数组推送来为DeliveryArea添加值。所有的值都将来自php变量。我是使用js的初学者,想要学习和入侵JS。
var Category = {
"Status": ["Unpaid", "Paid", "Pending"],
"OrderDate": ["123", "123", "123"],
"DeliveryArea": [],
}发布于 2018-10-29 14:54:43
使用对象名和键名,如下所示的Category.DeliveryArea
var Category = {
"Status": ["Unpaid", "Paid", "Pending"],
"OrderDate": ["123", "123", "123"],
"DeliveryArea": [],
}
let arr = ['arr1', 'arr2', 'arr3'];
arr.forEach(function(item) {
Category.DeliveryArea.push(item)
})
console.log(Category)
如果希望通过变量访问密钥,请使用方括号
var Category = {
"Status": ["Unpaid", "Paid", "Pending"],
"OrderDate": ["123", "123", "123"],
"DeliveryArea": [],
}
let keyName = 'DeliveryArea'
let arr = ['arr1', 'arr2', 'arr3'];
arr.forEach(function(item) {
Category[keyName].push(item)
})
console.log(Category)
https://stackoverflow.com/questions/53040184
复制相似问题