如何将具有键/值的对象转换为对象数组?

时间:2019-04-05 05:28:44

标签: javascript arrays javascript-objects

将具有键值的Javascript对象赋予对象数组?

示例:

var data = { id: 1, first_name: "George", last_name: "Bluth", avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg" }

结果应为:

[
  { id: 1, first_name: "George", last_name: "Bluth", avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg" }
]

3 个答案:

答案 0 :(得分:0)

只需itemName. propertyName. valueName. Q5. P2. Q1002. ...

[data]

答案 1 :(得分:0)

如果您有println 'Simulation AUV-3: Triangular motion (with dive)' simulate 15.minutes, { def n = node('AUV-3', location: [-50.m, -50.m, 0], mobility: true) n.startup = trackAuvLocation n.motionModel = [[time: 0.minutes, location: [-50.m, -50.m, 0] ], [time: 3.minutes, location: [-100.m, -50.m, 0] ], [time: 4.minutes, location: [-100.m, -100.m, 0] ], [time: 7.minutes, location: [-50.m, -100.m, 0] ], [time: 10.minutes, location: [-50.m, -50.m, 0] ]] } ,并希望将其放在数组中

data

查看摘要

data = [data];

答案 2 :(得分:0)

您可以创建一个数组,并使用data变量对其进行初始化:

const data = { id: 1, first_name: "George", last_name: "Bluth", avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg" };
const result = [data];

console.log(result);