将带有对象数组的javascript对象格式化为Array of Array

时间:2018-11-13 00:34:45

标签: javascript arrays json object

我有下面的对象,其中包含对象数组。我想将此对象格式化为数组数组的对象

var object1 = {
  "Usa": [{
    "period": "2018-11-03T00:00:00.000+0000",
    "qty": 1
  }, {
    "period": "2018-11-04T00:00:00.000+0000",
    "qty": 2
  }],
  "india": [
    {
      "period": "2018-19-03T00:00:00.000+0000",
      "qty": 2
    }, {
      "period": "2018-19-04T00:00:00.000+0000",
      "qty": 3
    }
  ]
}

export const createDataPoint = (period, qty) => {
  return [
    Date.now(time),
    Math.round((Math.random() * 100) * 2) / 2 + quantity,

  ];
};

export function createData(object1) {
  let data = [];
  let total = [];
  Object.keys(object1).map(function(item, index) {
    object1[item].map(function(item2, index2) {
      data.push(createDataPoint(item2.period, item2.qty));
    })
    object1[item] = data
  })
  // console.log(object1)
  return object1;
}

但是输出是,在每个数组中有4个数组,而不是2个相应的数组。这意味着在每个数组中,我将获得大小为4的数组。

预期输出为

  var object1={
     "Usa":[
      ["123245235",1],
      ["21423435",2]
     ],
    "india":[   
       ["234325436",2],
        ["23422464",3]
    ]    
  }

3 个答案:

答案 0 :(得分:1)

只需将每个数组分配给新的映射数组

const createDataPoint = ({period, qty})=>{
    // do your processing here, just returning period & qty for simplicity 
    return [period, qty]
}

Object.keys(data).forEach(k=>{
    data[k] = data[k].map(createDataPoint);
})

console.log(data)
<script>
  const data = {
    "Usa": [{
      "period": "2018-11-03T00:00:00.000+0000",
      "qty": 1
    }, {
      "period": "2018-11-04T00:00:00.000+0000",
      "qty": 2
    }],
    "india": [{
        "period": "2018-19-03T00:00:00.000+0000",
        "qty": 2
      }, {
        "period": "2018-19-04T00:00:00.000+0000",
        "qty": 3
      }

    ]
  }
</script>

答案 1 :(得分:1)

这是使用功能Object.entries来获取[country, array]之后的条目,然后使用功能reduce构建所需输出的一种选择。

函数map将生成带有两个索引的数组。

这种方法不会变异原始对象

var object1={  "Usa":[       {"period": "2018-11-03T00:00:00.000+0000",               "qty": 1           }, {               "period": "2018-11-04T00:00:00.000+0000",               "qty": 2           } ], "india":[   {    "period": "2018-19-03T00:00:00.000+0000",               "qty": 2           }, {               "period": "2018-19-04T00:00:00.000+0000",               "qty": 3           } ]    },
    result = Object.entries(object1).reduce((a, [country, arr]) => {
      return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
    }, Object.create(null));

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

答案 2 :(得分:1)

您可以使用Array.prototype.reduceObject.entries

var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }

const result = Object.entries(obj)
  .reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)

可以通过Object.keysreduce完成一些简单的方法:

var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }

const result = Object.keys(obj)
  .reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)