-1

Ive been struggling for hours to construct a json array but with no success whatsoever. For someone who has experience in json would be a piece of a cake for him.

So i want to construct a json array like this:

 { 
    "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"} ] 
 }

Here is the code:

 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);

    }

But instead i have this output

 { 
    "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"} ] 
 }

How can i put a value in index variable?

Thanks for any help

mathew
  • 185
  • 4
  • 20
  • 1
    [How to create object property from variable value?](http://stackoverflow.com/questions/2241875/how-to-create-object-property-from-variable-value-in-javascript) – DCoder Oct 14 '12 at 09:28
  • so set a value in selectedClothe[index] ? There is an error index undefined.. – mathew Oct 14 '12 at 09:35
  • 1
    Well yes, you have to declare a variable named `index` and set its value to `M`, `L`, `XL` or other before using it... – DCoder Oct 14 '12 at 09:41

2 Answers2

1

Try with:

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;

    }
Fabrizio Fortino
  • 1,479
  • 1
  • 14
  • 22
0

Use this. Assuming that the end clothe ID name after the _ character is the clothe size.

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"');
}
Jay
  • 4,627
  • 1
  • 21
  • 30