我有一个对象,如下所示:
dum2a
我有一个数组,其中包含该格式的多个对象。
我想从该数组创建一个新的对象数组,其中将包含以下格式的对象:
{
"id": 1,
"dataLockVersion": 0,
"auditData": {
"createDate": "2018-09-18T11:41:28.362",
"createUser": "XXX",
"updateDate": null,
"updateUser": null
},
"property1": 14021,
"property2": {...},
"property3": "Obj"
}
这是我尝试过的:
var结果= [];
{
"property1": 14021,
"property2": {...},
"property3": "Obj"
}
有更好的方法吗?
答案 0 :(得分:3)
使用地图和对象分解
const result = array
.map(
({ property1, property2, property3 })
=> ({ property1, property2, property3 }));
答案 1 :(得分:1)
您只需为此使用Array.map()和对象destructuring:
let arr =[{ "id": 1, "dataLockVersion": 0, "auditData": { "createDate": "2018-09-18T11:41:28.362", "createUser": "XXX", "updateDate": null, "updateUser": null }, "property1": 14021, "property2": {"x" :1}, "property3": "Obj" }, { "id": 1, "dataLockVersion": 0, "auditData": { "createDate": "2018-09-18T11:41:28.362", "createUser": "XXX", "updateDate": null, "updateUser": null }, "property1": 14021, "property2": {"x" :12}, "property3": "Obj" }];
let result = arr.map(({property1,property2,property3})=>Object.assign({},{property1,property2,property3}));
console.log(result);
答案 2 :(得分:1)
我会使用lodash.pick作为一种简单高效的解决方案。
通常,在应用程序的其他部分中会需要这种逻辑。
在您的情况下,它将是:
var newArrayWithPickedProperties = array.map(item => {
return _.pick(item, ['property1', 'property2', 'property3']);
})
如果您采用这种方式,请确保您import only lodash.pick不是整个lodash库。
答案 3 :(得分:0)
您可以尝试以下操作:
var data = [
{"id": 1,"dataLockVersion": 0,"auditData": {"createDate": "2018-09-18T11:41:28.362","createUser": "XXX","updateDate": null,"updateUser": null},"property1": 14021,"property2": {},"property3": "Obj"},
{"id": 2,"dataLockVersion": 1,"auditData": {"createDate": "2018-09-18T11:41:28.362","createUser": "YYY","updateDate": null,"updateUser": null},"property1": 140221,"property2": {},"property3": "Obj3"}
];
var res = data.map(function(m){return {property1: m.property1, property2: m.property2, property3: m.property3};})
console.log(res);
或者,如果您喜欢技巧和,所有值都是string
或number
或包含它们的对象,则可以使用它(不建议使用非常重的对象):
let data = [
{"id": 1,"dataLockVersion": 0,"auditData": {"createDate": "2018-09-18T11:41:28.362","createUser": "XXX","updateDate": null,"updateUser": null},"property1": 14021,"property2": {},"property3": "Obj"},
{"id": 2,"dataLockVersion": 1,"auditData": {"createDate": "2018-09-18T11:41:28.362","createUser": "YYY","updateDate": null,"updateUser": null},"property1": 140221,"property2": {},"property3": "Obj3"}
];
var res=[];
JSON.stringify(data).replace(/"(property1)"\:(.+?),.+?"(property\d+)"\:(.+?)(?=})/gi, function(a){res.push(JSON.parse("{"+a+"}"));});
console.log(res);
答案 4 :(得分:0)
如果数据来自JSON字符串,则JSON.parse
reviver parameter可用于排除属性:
var json = '{"id":1,"dataLockVersion":0,"auditData":{"createDate":"2018-09-18T11:41:28.362","createUser":"XXX","updateDate":null,"updateUser":null},"property1":14021,"property2":"{...}","property3":"Obj"}'
var obj = JSON.parse(json, (key, value) => /id|data/i.test(key) ? void 0 : value)
console.log( obj )
答案 5 :(得分:0)
如果您有n个具有固定的前3个键的属性,则可以进行解构分配。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
let data = [
{"id": 1,"dataLockVersion": 0,"auditData": {"createDate": "2018-09-18T11:41:28.362","createUser": "XXX","updateDate": null,"updateUser": null},"property1": 14021,"property2": {},"property3": "Obj","property4":"yo","property5":"hey"},
{"id": 2,"dataLockVersion": 1,"auditData": {"createDate": "2018-09-18T11:41:28.362","createUser": "YYY","updateDate": null,"updateUser": null},"property1": 140221,"property2": {},"property3": "Obj3"}
];
const arr=data.map(a=>{
let {id,dataLockVersion,auditData,...props}=a
return props;
}
)
console.log(arr);