3

I have this Javascript code with a JSON variable that works well.

var valuesJSON = {
    projects: {
        0: {value: 0, price: 0},
        1: {value: 0, price: 0},
        2: {value: 0, price: 0},
        3: {value: 0, price: 0},
        4: {value: 0, price: 0},
        5: {value: 0, price: 0},
        6: {value: 0, price: 0},
        7: {value: 0, price: 0},
    }
  }

I want to make it more scalable, so that it will read the number of entries from a constant. I tried the following but the for loop in the variable is not allowed. Any ideas?

const NUM_PROJECTS = 8
var valuesJSON = {
    projects: {
        for (var i=0; i < NUM_PROJECTS; i++) {
            i: {value: 0, price: 0},
        }
    }
  }
4
  • 2
    Use bracket notation if variable is to be used as key! Commented Dec 12, 2016 at 10:26
  • var valuesJSON = { projects: { } }; for (var i=0; i < NUM_PROJECTS; i++) { valuesJSON.projects[i]= {value: 0, price: 0} } Commented Dec 12, 2016 at 10:28
  • 5
    If all the keys are consecutive and numeric, why isn't projects an array? Commented Dec 12, 2016 at 10:28
  • As in var valuesJSON = { projects: [ {value: 0, price: 0}, {value: 0, price: 0}, {value: 0, price: 0}, {value: 0, price: 0}, {value: 0, price: 0}, {value: 0, price: 0}, {value: 0, price: 0}, {value: 0, price: 0} /* No trailing comma */ ] } Commented Dec 12, 2016 at 10:41

3 Answers 3

2

You need to add properties to projects using for loop. Bracket notation needs to used since variable is being used as key.

//Create object
var valuesJSON = {
   projects : {}
}

//Iterate and add properties
for (var i = 0; i < NUM_PROJECTS; i++) {
    valuesJSON.projects[i] = {
        value: 0,
        price: 0
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Beat you by 8 seconds
1

You could iterate with NUM_PROJECTS and generate new properties with the wanted result.

const NUM_PROJECTS = 8
var valuesJSON = { projects: {}};

for (var i = 0; i < NUM_PROJECTS; i++) {
    valuesJSON.projects[i] = { value: 0, price: 0 };
}

console.log(valuesJSON);

Comments

1

You first make a javascript object.

var object = {};

You can then edit properties( object.something) via array access.

 object.something == object['something']

so to set a value in an object, you can do

  object['something'] = array_counter

Not sure what you wanted, so I added both arrayinfying to an object and putting it in as an object.

var json = {}
var projects = {};
var projectsArr = [];
for(c=0;c<100;c++) {
     var project_entry = {
                              name: 'project'+c,
                              a:c+1,
                              b:c*2
                              };    
         
     projects[project_entry.name] = project_entry;

     projectsArr.push(project_entry);
}
json['projects'] = projects;
json['projects_as_array'] = projectsArr;
console.log(JSON.stringify(json,null,2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.