Javascript 构造Json数组时遇到的问题
我花了几个小时努力构建json数组,但没有任何成功。对于有json经验的人来说,这对他来说是小菜一碟 所以我想构建一个json数组,如下所示:Javascript 构造Json数组时遇到的问题,javascript,json,Javascript,Json,我花了几个小时努力构建json数组,但没有任何成功。对于有json经验的人来说,这对他来说是小菜一碟 所以我想构建一个json数组,如下所示: { "M" : [ {id:"58893_1_M", value:"Ontario", imageFile:"58893_1.jpg"} ] , "L" : [ {id:"58893_1_L", value:"Ontario", imageFile:"58893_1.jpg"} ] , "XL" : [ {id:"58893
{
"M" : [ {id:"58893_1_M", value:"Ontario", imageFile:"58893_1.jpg"} ] ,
"L" : [ {id:"58893_1_L", value:"Ontario", imageFile:"58893_1.jpg"} ] ,
"XL" : [ {id:"58893_1_XL", value:"Ontario", imageFile:"58893_1.jpg"} ]
}
代码如下:
var totalObjects = new Array();
for (i = 0; i < roomQuotes.length; i++) {
var selectedClothe = {
index: []
};
var clotheId = some value;
var clotheQuantity = some value;
var clotheImage =some value;
selectedClothe.index.push({ "id": clotheId , "value": clotheQuantity , "imageFile": clotheImage });
totalObjects.push(selectedClothe);
}
如何在索引变量中输入值
感谢您的帮助请尝试:
var totalObjects = {};
for (i = 0; i < roomQuotes.length; i++) {
var selectedClothe = [];
var clotheId = some value;
var clotheQuantity = some value;
var clotheImage =some value;
selectedClothe.push({ "id": clotheId , "value": clotheQuantity , "imageFile": clotheImage });
totalObjects.[clotheId.substr(clotheId.lastIndexOf('_') +1)] = selectedClothe;
}
var totalObjects={};
对于(i=0;i
使用此选项。假设字符后面的结束衣物ID名称是衣物尺寸
var totalObjects = {}; //selection object
for (i = 0; i < roomQuotes.length; i++) {
var clotheId = some value;
var clotheQuantity = some value;
var clotheImage = some value;
var clotheSize = clotheId.substr(clotheId.lastIndexOf('_')+1);
if (typeof(totalObjects[clotheSize]) == 'undefined') {
//clothe size array not yet exist. create it
totalObjects[clotheSize] = []; //clothe size array
}
totalObjects[clotheSize].push({ "id": clotheId , "value": clotheQuantity , "imageFile": clotheImage });
}
//note: there will be no "totalObjects.XL" if there's no selected clothe of "XL" size
//example: list selected clothe sizes
//see web browser's Error Console for console.log result
var clotheSizes = Object.keys(totalObjects); //clothe size code array
console.log('Selected clothe sizes: '+clotheSizes.join(', '));
//shows e.g.: "M, L, XL" or "" if no selection
//example: get first selected clothe ID of first clothe size selection
if (clotheSize.length > 0) {
var clothSizeSelections = totalObjects[clotheSizes[0]];
console.log('First selected clothe ID: '+clothSizeSelections[0].id);
} else {
console.log('No selection');
}
//example: is "M" clothe size has selection?
if (typeof(totalObjects.M) != 'undefined') {
console.log(totalObjects.M.length+' selections for clothe size "M"');
} else {
console.log('No selection for clothe size "M"');
}
var totalObjects={}//选择对象
对于(i=0;i0){
var clothSizeSelections=totalObjects[ClothSizes[0]];
console.log('第一个选择的衣服ID:'+clothSizeSelections[0].ID);
}否则{
console.log(“无选择”);
}
//示例:“M”衣服尺寸有选择吗?
if(typeof(totalObjects.M)!=“未定义”){
console.log(totalObjects.M.length+'衣服尺寸“M”的选择);
}否则{
console.log('衣服尺寸“M”没有选择');
}
所以在SelectedCloth[索引]中设置一个值?有一个错误索引未定义。是的,您必须声明一个名为index
的变量,并将其值设置为M
、L
、XL
或其他,然后才能使用它。。。
var totalObjects = {}; //selection object
for (i = 0; i < roomQuotes.length; i++) {
var clotheId = some value;
var clotheQuantity = some value;
var clotheImage = some value;
var clotheSize = clotheId.substr(clotheId.lastIndexOf('_')+1);
if (typeof(totalObjects[clotheSize]) == 'undefined') {
//clothe size array not yet exist. create it
totalObjects[clotheSize] = []; //clothe size array
}
totalObjects[clotheSize].push({ "id": clotheId , "value": clotheQuantity , "imageFile": clotheImage });
}
//note: there will be no "totalObjects.XL" if there's no selected clothe of "XL" size
//example: list selected clothe sizes
//see web browser's Error Console for console.log result
var clotheSizes = Object.keys(totalObjects); //clothe size code array
console.log('Selected clothe sizes: '+clotheSizes.join(', '));
//shows e.g.: "M, L, XL" or "" if no selection
//example: get first selected clothe ID of first clothe size selection
if (clotheSize.length > 0) {
var clothSizeSelections = totalObjects[clotheSizes[0]];
console.log('First selected clothe ID: '+clothSizeSelections[0].id);
} else {
console.log('No selection');
}
//example: is "M" clothe size has selection?
if (typeof(totalObjects.M) != 'undefined') {
console.log(totalObjects.M.length+' selections for clothe size "M"');
} else {
console.log('No selection for clothe size "M"');
}