如何遍历一个对象数组,将键推送到另一个数组

时间:2018-06-13 06:38:38

标签: javascript arrays

我有以下数组,我想循环遍历它并将键值推送到另一个声明的数组。然后,遍历数组并将字段和键的值推送到另一个数组。

timeframe = [];

timeframeValueKeyUSD = [];

timeframeValueKeyCAD = [];

array = [
       {
      "2012-05-01":{
        "USD": 1.322891,
        "AUD": 1.278047,
        "CAD": 1.302303
      },
      "2012-05-02": {
        "USD": 1.315066,
        "AUD": 1.274202,
        "CAD": 1.299083
      },
      "2012-05-03": {
        "USD": 1.314491,
        "AUD": 1.280135,
        "CAD": 1.296868
      }
  }
]

我希望得到这样的数组:

timeframe = ['2012-05-01', '2012-05-02', '2012-05-03'];

timeframeValueKeyUSD = [1.315066, 1.315066, 1.314491];

timeframeValueKeyCAD = [1.302303, 1.299083, 1.296868];

4 个答案:

答案 0 :(得分:2)

您可以先获取日期,然后根据日期映射值。



function getKeys(o) {
    return Object.keys(o);
}

function getValueBy(object, groups, key) {
    return groups.map(k => object[k][key]);
}

var array = [{ "2012-05-01":{ USD: 1.322891, AUD: 1.278047, CAD: 1.302303 }, "2012-05-02": { USD: 1.315066, AUD: 1.274202, CAD: 1.299083 }, "2012-05-03": { USD: 1.314491, AUD: 1.280135, CAD: 1.296868 } }],
    timeframe = getKeys(array[0]);
    timeframeValueKeyUSD = getValueBy(array[0], timeframe, 'USD'),
    timeframeValueKeyCAD = getValueBy(array[0], timeframe, 'CAD');

console.log(timeframe);
console.log(timeframeValueKeyUSD);
console.log(timeframeValueKeyCAD);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:1)

使用简单的forEach()循环来获得所需的输出。

timeframe = [];

var timeframeValueKeyUSD = [];

var timeframeValueKeyCAD = [];

var array = [
       {
      "2012-05-01":{
        "USD": 1.322891,
        "AUD": 1.278047,
        "CAD": 1.302303
      },
      "2012-05-02": {
        "USD": 1.315066,
        "AUD": 1.274202,
        "CAD": 1.299083
      },
      "2012-05-03": {
        "USD": 1.314491,
        "AUD": 1.280135,
        "CAD": 1.296868
      }
  }
]

array.forEach((obj)=>{
  var keys = Object.keys(obj);
  timeframe.push(...keys);
  keys.forEach((key)=>{
     timeframeValueKeyUSD.push(obj[key].USD);
     timeframeValueKeyCAD.push(obj[key].CAD);
  });
});

console.log(timeframe);
console.log(timeframeValueKeyUSD);
console.log(timeframeValueKeyCAD);

答案 2 :(得分:1)

您可以使用for循环遍历:



var timeframe = [];
var timeframeValueKeyUSD = [];
var timeframeValueKeyCAD = [];
var array = [{"2012-05-01":{"USD":1.322891,"AUD":1.278047,"CAD":1.302303},"2012-05-02":{"USD":1.315066,"AUD":1.274202,"CAD":1.299083},"2012-05-03":{"USD":1.314491,"AUD":1.280135,"CAD":1.296868}}];
for (var t in array[0]) {
  timeframe.push(t);
  timeframeValueKeyUSD.push(array[0][t].USD);
  timeframeValueKeyCAD.push(array[0][t].CAD);
}
console.log(timeframe, timeframeValueKeyUSD, timeframeValueKeyCAD);




答案 3 :(得分:0)

较短的ES6声明替代方案:

const dataObj = array[0];
const { timeframe, timeframeValueKeyUSD, timeframeValueKeyCAD } = Object.keys(dataObj).reduce((all, timeframe) => {

    const { USD, CAD } = dataObj[timeframe];

    all.timeframe.push(timeframe);
    all.timeframeValueKeyUSD.push(USD);
    all.timeframeValueKeyCAD.push(CAD);

    return all;

}, {timeframe: [], timeframeValueKeyUSD: [], timeframeValueKeyCAD: []});

console.log(timeframe);
console.log(timeframeValueKeyUSD);
console.log(timeframeValueKeyCAD);

如果你想让它变得更有活力(涵盖所有可能的货币):

const dataObj = array[0];
const result = Object.keys(dataObj).reduce((all, timeframe) => {

    const currencies = Object.keys(dataObj[timeframe]);

    all.timeframe.push(timeframe);

    currencies.forEach(currency => {

        if (!all.hasOwnProperty(`timeframeValueKey${currency}`)) {
            all[`timeframeValueKey${currency}`] = [];
        }

        all[`timeframeValueKey${currency}`].push(dataObj[timeframe][currency]);

    });

    return all;

}, {timeframe: [] });


console.log(result);

timeframe:(3) ["2012-05-01", "2012-05-02", "2012-05-03"]
timeframeValueKeyAUD:(3) [1.278047, 1.274202, 1.280135]
timeframeValueKeyCAD:(3) [1.302303, 1.299083, 1.296868]
timeframeValueKeyUSD:(3) [1.322891, 1.315066, 1.314491]