Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8678421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:43:01+00:00 2026-06-12T20:43:01+00:00

Ive been struggling for hours to construct a json array but with no success

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:43:02+00:00Added an answer on June 12, 2026 at 8:43 pm

    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"');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been looking for this answer for hours, but I cant find it. I
I've been struggling with this for hours, but I couldn't manage to find out
I've been struggling with this for a couple of hours, and I'm hoping someone
I've been struggling with for a couple of hours but I'm sure it's dead
I 've been struggling for more than 3 hours but cannot find a solution.
So i feel like a noob but this has been baffling me or hours
I've been struggling with this for several hours and can't seem to figure out
I've been struggling with this for a couple of hours now and I simply
Im struggling with Valumx exellent Fileupload. Ive been using it for a couple of
I've been struggling with this for the past couple hours now and I really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.