无法将对象转换为数组

时间:2019-04-05 15:47:48

标签: javascript arrays object type-conversion

我正在尝试将对象转换为js中的数组。

我尝试了以下一些操作。什么都行不通。

var obj = [{"date":"1527206400","L":"3"},{"date":"1527811200","L":"2"},{"date":"1528416000","L":"2"},{"date":"1529020800","L":"3"},{"date":"1529625600","L":"3"},{"date":"1530057600","L":"3"},{"date":"1530835200","L":"3"},{"date":"1532044800","L":"3"},{"date":"1532649600","L":"3"},{"date":"1533254400","L":"3"},{"date":"1533859200","L":"3"},{"date":"1534464000","L":"3"},{"date":"1535068800","L":"3"},{"date":"1535673600","L":"3"},{"date":"1536278400","L":"3"},{"date":"1536883200","L":"2"},{"date":"1537488000","L":"3"},{"date":"1538092800","L":"3"},{"date":"1538697600","L":"2"},{"date":"1539302400","L":"3"},{"date":"1539907200","L":"2"},{"date":"1540512000","L":"3"},{"date":"1541116800","L":"2"},{"date":"1541721600","L":"2"},{"date":"1542326400","L":"1"},{"date":"1542931200","L":"-1"},{"date":"1543536000","L":"0"},{"date":"1544140800","L":"0"},{"date":"1544745600","L":"0"},{"date":"1545350400","L":"0"},{"date":"1545955200","L":"0"},{"date":"1546560000","L":"1"},{"date":"1547164800","L":"1"},{"date":"1547769600","L":"0"},{"date":"1548374400","L":"0"},{"date":"1548979200","L":"-1"},{"date":"1549584000","L":"0"},{"date":"1550188800","L":"-1"},{"date":"1550793600","L":"-1"},{"date":"1551398400","L":"-1"},{"date":"1552003200","L":"-1"},{"date":"1552608000","L":"-1"},{"date":"1553212800","L":"0"},{"date":"1553817600","L":"0"}];

var arr = Array.from(Object.keys(obj), k=>obj[k]);

console.log(arr)

我想要

arr = [[1527206400,3][1527811200,2]]

我已从这篇文章中获得了一些帮助,但无济于事: How to Convert...

5 个答案:

答案 0 :(得分:4)

You can use map to loop thru the array and return a new ordered array.

var obj = [{"date":"1527206400","L":"3"},{"date":"1527811200","L":"2"},{"date":"1528416000","L":"2"},{"date":"1529020800","L":"3"},{"date":"1529625600","L":"3"},{"date":"1530057600","L":"3"},{"date":"1530835200","L":"3"},{"date":"1532044800","L":"3"},{"date":"1532649600","L":"3"},{"date":"1533254400","L":"3"},{"date":"1533859200","L":"3"},{"date":"1534464000","L":"3"},{"date":"1535068800","L":"3"},{"date":"1535673600","L":"3"},{"date":"1536278400","L":"3"},{"date":"1536883200","L":"2"},{"date":"1537488000","L":"3"},{"date":"1538092800","L":"3"},{"date":"1538697600","L":"2"},{"date":"1539302400","L":"3"},{"date":"1539907200","L":"2"},{"date":"1540512000","L":"3"},{"date":"1541116800","L":"2"},{"date":"1541721600","L":"2"},{"date":"1542326400","L":"1"},{"date":"1542931200","L":"-1"},{"date":"1543536000","L":"0"},{"date":"1544140800","L":"0"},{"date":"1544745600","L":"0"},{"date":"1545350400","L":"0"},{"date":"1545955200","L":"0"},{"date":"1546560000","L":"1"},{"date":"1547164800","L":"1"},{"date":"1547769600","L":"0"},{"date":"1548374400","L":"0"},{"date":"1548979200","L":"-1"},{"date":"1549584000","L":"0"},{"date":"1550188800","L":"-1"},{"date":"1550793600","L":"-1"},{"date":"1551398400","L":"-1"},{"date":"1552003200","L":"-1"},{"date":"1552608000","L":"-1"},{"date":"1553212800","L":"0"},{"date":"1553817600","L":"0"}];

var result = obj.map(o=> [o.date, o.L]);

console.log(result);


..Or you can use Object.values to convert the object into an array. But this method does not guarantee the order of the inner array.

var obj = [{"date":"1527206400","L":"3"},{"date":"1527811200","L":"2"},{"date":"1528416000","L":"2"},{"date":"1529020800","L":"3"},{"date":"1529625600","L":"3"},{"date":"1530057600","L":"3"},{"date":"1530835200","L":"3"},{"date":"1532044800","L":"3"},{"date":"1532649600","L":"3"},{"date":"1533254400","L":"3"},{"date":"1533859200","L":"3"},{"date":"1534464000","L":"3"},{"date":"1535068800","L":"3"},{"date":"1535673600","L":"3"},{"date":"1536278400","L":"3"},{"date":"1536883200","L":"2"},{"date":"1537488000","L":"3"},{"date":"1538092800","L":"3"},{"date":"1538697600","L":"2"},{"date":"1539302400","L":"3"},{"date":"1539907200","L":"2"},{"date":"1540512000","L":"3"},{"date":"1541116800","L":"2"},{"date":"1541721600","L":"2"},{"date":"1542326400","L":"1"},{"date":"1542931200","L":"-1"},{"date":"1543536000","L":"0"},{"date":"1544140800","L":"0"},{"date":"1544745600","L":"0"},{"date":"1545350400","L":"0"},{"date":"1545955200","L":"0"},{"date":"1546560000","L":"1"},{"date":"1547164800","L":"1"},{"date":"1547769600","L":"0"},{"date":"1548374400","L":"0"},{"date":"1548979200","L":"-1"},{"date":"1549584000","L":"0"},{"date":"1550188800","L":"-1"},{"date":"1550793600","L":"-1"},{"date":"1551398400","L":"-1"},{"date":"1552003200","L":"-1"},{"date":"1552608000","L":"-1"},{"date":"1553212800","L":"0"},{"date":"1553817600","L":"0"}];

var result = obj.map(o=> Object.values(o));

console.log(result);


答案 1 :(得分:3)

So, something like

obj.map(({ date, L }) => [date, L]);

答案 2 :(得分:1)

您可以为此使用简单地图:

const obj = [{"date":"1527206400","L":"3"},{"date":"1527811200","L":"2"}];

console.log(obj.map(({date,L})=>[date,L]));

答案 3 :(得分:1)

var obj = [{"date":"1527206400", "L":"3"}, {"date":"1527811200", "L":"2"}, {"date":"1528416000", "L":"2"}, {"date":"1529020800", "L":"3"}, {"date":"1529625600", "L":"3"}, {"date":"1530057600", "L":"3"}, {"date":"1530835200", "L":"3"}, {"date":"1532044800", "L":"3"}, {"date":"1532649600", "L":"3"}, {"date":"1533254400", "L":"3"}, {"date":"1533859200", "L":"3"}, {"date":"1534464000", "L":"3"}, {"date":"1535068800", "L":"3"}, {"date":"1535673600", "L":"3"}, {"date":"1536278400", "L":"3"}, {"date":"1536883200", "L":"2"}, {"date":"1537488000", "L":"3"}, {"date":"1538092800", "L":"3"}, {"date":"1538697600", "L":"2"}, {"date":"1539302400", "L":"3"}, {"date":"1539907200", "L":"2"}, {"date":"1540512000", "L":"3"}, {"date":"1541116800", "L":"2"}, {"date":"1541721600", "L":"2"}, {"date":"1542326400", "L":"1"}, {"date":"1542931200", "L":"-1"}, {"date":"1543536000", "L":"0"}, {"date":"1544140800", "L":"0"}, {"date":"1544745600", "L":"0"}, {"date":"1545350400", "L":"0"}, {"date":"1545955200", "L":"0"}, {"date":"1546560000", "L":"1"}, {"date":"1547164800", "L":"1"}, {"date":"1547769600", "L":"0"}, {"date":"1548374400", "L":"0"}, {"date":"1548979200", "L":"-1"}, {"date":"1549584000", "L":"0"}, {"date":"1550188800", "L":"-1"}, {"date":"1550793600", "L":"-1"}, {"date":"1551398400", "L":"-1"}, {"date":"1552003200", "L":"-1"}, {"date":"1552608000", "L":"-1"}, {"date":"1553212800", "L":"0"}, {"date":"1553817600", "L":"0"}];
var arr = obj.map(function(e){ return [e.date,e.L] });
console.log(arr)

答案 4 :(得分:0)

您可以遍历每个对象的键并将值推入数组,然后将该数组推入新数组。

var arr = [
  {"date":"1527206400","L":"3"},{"date":"1527811200","L":"2"},
  {"date":"1528416000","L":"2"},{"date":"1529020800","L":"3"},
  {"date":"1529625600","L":"3"}
]

var newArr = [];

arr.forEach(function(x) {
  var elemArr = [];
  for (var key in x) {
    elemArr.push(x[key]);
  }
  newArr.push(elemArr);
})

console.log(newArr);