jQuery对象数组中如何推送数组对象元素



我正在尝试创建自定义产品订单,我需要将所有选择的选项存储在JSON中。

这样的

let attr = [{
fabric: 'Bargandi Check',
style:[{
Jacket lapels: 'Notch'
}],
}];

用于理解面糊的选择器图像

我希望当用户选择任何这些选项时,我将存储在JSON中当用户更新任何选项时,它将在JSON对象中更新当用户选择新选项时,它将添加JSON

这样的

let attr = [{
fabric: 'Bargandi Check',
style:[{
Jacket lapels: 'Notch',
Lapel width: 'Slim' // this
}],
}];

你不能"将元素放入Javascript对象中。但是,您可以使用点操作符或方括号来访问现有的属性/重写属性/分配属性

const obj = { name: 'ed', age: '16' };
// rewriting an existing property
obj["age"] = 20; 
obj["name"] = 'edward'; 
console.log(obj["skill"]); // will be undefined
obj["skill"] = "Javascript"; // setting it as a new property
obj["list"] = []; // the list property is an array
obj["list"].push(1); // now i can use push because its an array
console.log(obj);

还有一点,javascript属性不能有空格,但你可以用连字符或下划线分隔

"对象属性名可以是任何有效的JavaScript字符串,或者任何可以转换为字符串的东西,包括空字符串。但是,任何不是有效JavaScript标识符的属性名(例如,属性名中有空格或连字符,或者以数字开头)只能使用方括号符号&quot来访问;——MDN

你的校正对象

let attr = [{
fabric: 'Bargandi Check',
style: [{
Jacket_lapels: 'Notch',
Lapel_width: 'Slim' // this
}],
}];

const found = attr.find(x => x.fabric == 'Bargandi Check');
found.style[0].Lapel_width = 'Thick' // this will rewrite the width property to "thick"
console.log(attr);