我的对象
{
bookingDate: "2019-10-17",
createDate: "2019-10-14T12:40:08.624",
lastUpdateTime: "2019-10-17T16:04:30.973",
p2p00: true,
p2p01: true,
p2p02: true,
p2p03: true,
p2p04: true,
response: null,
sa00: 0,
sa01: 0,
sa02: 0,
sa03: 0,
sa04: 0,
sa05: 0,
srr00: false,
srr01: false,
srr02: false,
srr03: false,
srr04: false,
srr05: false,
sup00: 0,
sup01: 0,
sup02: 0,
sup03: 0,
sup04: 0,
sup05: 0
}
我想做的通用对象数组
[
{p2p00: true, sa00: 0, srr00: false, sup00: 0},
{p2p01: true, sa01: 0, srr01: false, sup01: 0},
{p2p02: true, sa02: 0, srr02: false, sup02: 0},
{p2p03: true, sa03: 0, srr03: false, sup03: 0},
{p2p04: true, sa04: 0, srr04: false, sup04: 0}
]
如何使用对象中包含所需字段的对象组成数组? 我只想创建一个包含我想要的字段的数组
答案 0 :(得分:0)
执行以下操作:
var obj = {
bookingDate: "2019-10-17",
createDate: "2019-10-14T12:40:08.624",
lastUpdateTime: "2019-10-17T16:04:30.973",
p2p00: true,
p2p01: true,
p2p02: true,
p2p03: true,
p2p04: true,
response: null,
sa00: 0,
sa01: 0,
sa02: 0,
sa03: 0,
sa04: 0,
sa05: 0,
srr00: false,
srr01: false,
srr02: false,
srr03: false,
srr04: false,
srr05: false,
sup00: 0,
sup01: 0,
sup02: 0,
sup03: 0,
sup04: 0,
sup05: 0
};
var ar = [];
ar.push({'p2p00': obj.p2p00});
ar.push({'p2p04': obj.p2p04});
console.log(ar);
答案 1 :(得分:0)
您可以使用map
功能。例如:
const obj = {
name: 'John',
lastName: 'Doe',
age: 21
}
const newObj = obj.map(x => {
return {
name: x.name,
age: x.age
}
})
console.log(newObj); // output: { name: John, age: 21}
有关更多详细信息,您可以检查:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
答案 2 :(得分:0)
master
答案 3 :(得分:0)
const obj = {
bookingDate: "2019-10-17",
createDate: "2019-10-14T12:40:08.624",
lastUpdateTime: "2019-10-17T16:04:30.973",
p2p00: true,
p2p01: true,
p2p02: true,
p2p03: true,
p2p04: true,
response: null,
sa00: 0,
sa01: 0,
sa02: 0,
sa03: 0,
sa04: 0,
sa05: 0,
srr00: false,
srr01: false,
srr02: false,
srr03: false,
srr04: false,
srr05: false,
sup00: 0,
sup01: 0,
sup02: 0,
sup03: 0,
sup04: 0,
sup05: 0
};
let tmpObj = {}
for(let key in obj) {
let match = key.match('0[0-9]$');
if (match) {
if (!tmpObj[match[0]]) {
tmpObj[match[0]] = {}
}
tmpObj[match[0]][key] = obj[key];
}
}
const result = Object.values(tmpObj)