-1

私は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_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);

    }

しかし、代わりに私はこの出力を持っています

 { 
    "index" : [ {id:"58893_1_M", value:"Ontario", imageFile:"58893_1.jpg"} ] ,
    "index" : [ {id:"58893_1_L", value:"Ontario", imageFile:"58893_1.jpg"} ] , 
    "index" : [ {id:"58893_1_XL", value:"Ontario", imageFile:"58893_1.jpg"} ] 
 }

インデックス変数に値を入れるにはどうすればよいですか?

助けてくれてありがとう

4

2 に答える 2

1

試してみてください:

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;

    }
于 2012-10-14T09:48:19.423 に答える
0

これを使って。_文字の後の最後の服の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"');
}
于 2012-10-14T10:26:45.833 に答える