从JavaScript对象中提取值并按正确的顺序推送到数组

时间:2017-10-26 14:12:33

标签: javascript

我正在尝试使用从对象中提取的利率变量填充空数组。解决方案不正确,因为值的顺序不正确,即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

示例输出:[3.67, 3.71, 3.769, 3.672, etc]

1 个答案:

答案 0 :(得分:2)

字典(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);