[英]javascript array with keys containing another array of objects using php
I am working on an e-commerce project.我正在做一个电子商务项目。 the user can select multiple colors and can enter the quantity for each size of that color differently.
用户可以选择多种颜色,并可以为该颜色的每种尺寸输入不同的数量。 I want to store that data into an array and then send it to the server using ajax
我想将该数据存储到一个数组中,然后使用 ajax 将其发送到服务器
the array should look like this数组应该是这样的
[ {
'color' : [ {
'INDIGO' : [
{ 'S' : 15},
{ 'M' : 15},
] },{
'PURPLE' : [
{ 'S' : 15},
{ 'M' : 15},
]},
]}
]
but I am getting this type of array.但我得到了这种类型的数组。 it is inserting the last quantity I have inserted replaces the old one like if on purple color if I put 15 for s and 15 for m it only takes m
它正在插入我插入的最后一个数量替换旧的,就像紫色一样,如果我为 s 输入 15,为 m 输入 15 只需要 m
INDIGO: {L: 15} PURPLE: {M: 15}靛蓝:{L:15} 紫色:{M:15}
here is my code sample这是我的代码示例
when the user selects color radio it pushes the value into the color array当用户选择颜色单选时,它会将值推送到颜色数组中
var key = 'INDIGO';
color[key] = {};
and when the user increases quantity it inserts the object for the color key当用户增加数量时,它会插入颜色键的对象
var len = $('.radioCheckbox:checked');
for (var i = 0; i < len.length; i++) {
var key = len[i].value;
color[key] = {
[$size] : val,
};
}
It look like when you are putting the 15 for s and 15 for m you color is not rightly set for each size.看起来当您将 15 用于 s 并将 15 用于 m 时,您的颜色没有为每个尺寸正确设置。 So check when you are pushing m or s the color is set correctly.
因此,当您按下 m 或 s 时,请检查颜色设置是否正确。 Alternately, Not sure why do you need complex data structure.
或者,不确定为什么需要复杂的数据结构。 I think it can be achievable using data structure which is easy handle like
我认为可以使用易于处理的数据结构来实现,例如
data = {
'color': {
'INDIGO': {
'S': 15,
'M': 15
},
'PURPLE': {
'S': 15,
'M': 15
}
}
}
you can manage color, size and its values like,您可以管理颜色、大小及其值,例如,
data['color'][key][$size] = val
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.