简体   繁体   中英

Extract values from JavaScript object and push into array in the correct order

I'm trying to fill an empty array with interest rate variables extracted from an object. The solution is incorrect because the order of values is not correct ie Year 2015: 10th month's value, 11th, 12th, 1st, 2nd, 3rd ..9th . Then 2016: 10, 11, 12, 1,..7,8,9 etc Year 2015: 10th month's value, 11th, 12th, 1st, 2nd, 3rd ..9th . Then 2016: 10, 11, 12, 1,..7,8,9 etc . In other words it always extracts the 10th value first, not the 1st value. Below is sample data and my code.

Sample Data:

2015:
01:{interestrate: 3.67, count: 4}
02:{interestrate: 3.71, count: 4}
03:{interestrate: 3.7699999999999996, count: 4}
04:{interestrate: 3.6720000000000006, count: 5}
05:{interestrate: 3.84, count: 4}
06:{interestrate: 3.9825, count: 4}
07:{interestrate: 4.046, count: 5}
08:{interestrate: 3.905, count: 4}
09:{interestrate: 3.8899999999999997, count: 4}
10:{interestrate: 3.7959999999999994, count: 5}
11:{interestrate: 3.9425, count: 4}
12:{interestrate: 3.964, count: 5}

2016:
01:{interestrate: 3.8725000000000005, count: 4}
02:{interestrate: 3.66, count: 4}
03:{interestrate: 3.6940000000000004, count: 5}
04:{interestrate: 3.605, count: 4}
05:{interestrate: 3.6, count: 4}
06:{interestrate: 3.568, count: 5}
07:{interestrate: 3.4400000000000004, count: 4}
08:{interestrate: 3.435, count: 4}
09:{interestrate: 3.46, count: 5}
10:{interestrate: 3.47, count: 4}
11:{interestrate: 3.7699999999999996, count: 4}
12:{interestrate: 4.198, count: 5}

Code:

let arr = [];

for (x in result) {
  for (i in result[x]) {
    //console.log(result[x][i]['interestrate']);
    arr.push(result[x][i]['interestrate']);
  }
}

How can I rewrite code so that I get the interestrate values in this order 2015: 1,2,3,4,5,6,7,8,9,10,11,12. Then 2016: 1,2,3,4,5,6,7,8,9,10,11,12 Then 2017: etc 2015: 1,2,3,4,5,6,7,8,9,10,11,12. Then 2016: 1,2,3,4,5,6,7,8,9,10,11,12 Then 2017: etc .

Example output: [3.67, 3.71, 3.769, 3.672, etc]

Dictionaries (objects in Javascript works like dictionaries) are not sorted, so you must sort the keys before entering the loop.

Sample data:

let result = {
    2015: {
        1:{interestrate: 3.67, count: 4},
        2:{interestrate: 3.71, count: 4},
        3:{interestrate: 3.7699999999999996, count: 4},
        4:{interestrate: 3.6720000000000006, count: 5},
        5:{interestrate: 3.84, count: 4},
        6:{interestrate: 3.9825, count: 4},
        7:{interestrate: 4.046, count: 5},
        8:{interestrate: 3.905, count: 4},
        9:{interestrate: 3.8899999999999997, count: 4},
        10:{interestrate: 3.7959999999999994, count: 5},
        11:{interestrate: 3.9425, count: 4},
        12:{interestrate: 3.964, count: 5}},
    2016: {
        1:{interestrate: 3.8725000000000005, count: 4},
        2:{interestrate: 3.66, count: 4},
        3:{interestrate: 3.6940000000000004, count: 5},
        4:{interestrate: 3.605, count: 4},
        5:{interestrate: 3.6, count: 4},
        6:{interestrate: 3.568, count: 5},
        7:{interestrate: 3.4400000000000004, count: 4},
        8:{interestrate: 3.435, count: 4},
        9:{interestrate: 3.46, count: 5},
        10:{interestrate: 3.47, count: 4},
        11:{interestrate: 3.7699999999999996, count: 4},
        12:{interestrate: 4.198, count: 5}
}};

Code:

// If you just use sort(), the order will be wrong, because it will
// be sort alphabetically, and 11 would appear before 2.
function compareNumbers(a, b)
{
    return a - b;
}

let arr = [];

Object.keys(result).sort(compareNumbers).forEach(function(year) {
  Object.keys(result[year]).sort(compareNumbers).forEach(function(month) {
    arr.push(result[year][month]['interestrate']);
  });
});

console.log(arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM