[英]Extract values from JavaScript object and push into array in the correct order
我正在尝试使用从对象提取的利率变量填充一个空数组。 解决方案是不正确的,因为值的顺序不正确,即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
。 换句话说,它总是首先提取第10个值,而不是第1个值。 以下是示例数据和我的代码。
样本数据:
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}
码:
let arr = [];
for (x in result) {
for (i in result[x]) {
//console.log(result[x][i]['interestrate']);
arr.push(result[x][i]['interestrate']);
}
}
如何重写代码,以便按以下顺序获得利息值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
。
输出示例: [3.67, 3.71, 3.769, 3.672, etc]
字典(Javascript中的对象就像字典一样)未排序,因此必须在进入循环之前对键进行排序。
样本数据:
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}
}};
码:
// 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.