javascript - Add properties in object with the help of a for loop and arrays -


i trying add properties object come array. code using:

var data = {}; var arr_years = [2013, 2014, 2015]; var arr_quarters2013 = ['q2', 'q3', 'q4']; var arr_quarters2014 = ['q1', 'q2', 'q3', 'q4']; var arr_quarters2015 = ['q1', 'q2', 'q3', 'q4'];  function createyearobjects() {     (var in arr_years) {         data[arr_years[i]] = '';     } } 

i trying add quarters each year array right property in object. in 2013 have q2, q3 , q4. first try. thought keeping simple trick:

function fillyearobjectswithquarters() {     (var prop in data) {         data[prop] = arr_quarters[prop];     } } 

since did not work out, tried put string ideas ended in node giving me string 'arr_quarters2013' - 'arr_quarters2015' not want, want reference array name!

function fillyearobjectswithquarters() {     (var prop in data) {         var temp = prop.tostring();         var tempstring = 'arr_quarters'+temp.tostring();         console.log(tempstring);     } } 

also thought why not give shot , rid of possible async. task shouldn't me near async-problems still tried.

createyearobjects(); settimeout(function() {     fillyearobjectswithquarters(); }, 250); 

the solution according parameters you've provided, use of eval (not good)... jsfiddle code - eval solution

var data = {}; var arr_years = [2013, 2014, 2015]; var arr_quarters2013 = ['q2', 'q3', 'q4']; var arr_quarters2014 = ['q1', 'q2', 'q3', 'q4']; var arr_quarters2015 = ['q1', 'q2', 'q3', 'q4']; var current_year; var quarter_year;   function createyearobjects(yearsarray) {     (var i=0;i<yearsarray.length;i++) {         current_year = yearsarray[i];         quarter_year = eval("arr_quarters"+current_year);         //console.log("quarter_year: ",quarter_year);         if (current_year) {             data[current_year] = quarter_year;         }     } }  createyearobjects(arr_years);  var resultdiv = document.getelementbyid("result"); resultdiv.innerhtml = json.stringify(data); 

a better solution (without eval):

jsfiddle code - without eval solution

var data = {}; var arr_years = [2013, 2014, 2015]; var arr_quarters = {} arr_quarters["2013"] = ['q2', 'q3', 'q4']; arr_quarters["2014"] = ['q1', 'q2', 'q3', 'q4']; arr_quarters["2015"] = ['q1', 'q2', 'q3', 'q4']; var current_year; var quarter_year;  function createyearobjects(yearsarray) {     (var i=0;i<yearsarray.length;i++) {         current_year = yearsarray[i];         quarter_year = arr_quarters[current_year];         //console.log("quarter_year: ",quarter_year);         if (current_year) {             data[current_year] = quarter_year;         }     } }  createyearobjects(arr_years);  var resultdiv = document.getelementbyid("result"); resultdiv.innerhtml = json.stringify(data); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -