4

I have an object as follows:

 {
 "id": 1,
 "dataLockVersion": 0,
 "auditData": {
 "createDate": "2018-09-18T11:41:28.362",
 "createUser": "XXX",
 "updateDate": null,
 "updateUser": null
 },
 "property1": 14021,
 "property2": {...},
 "property3": "Obj"
 }

And I have an array that contains multiple objects in that format.

I want to create a new array of objects from this array, which will contain objects in this format :

{
 "property1": 14021,
 "property2": {...},
 "property3": "Obj"
 }

This is what I tried :

var result = [];

for (i = 0; i < arr.length; i++) {
 delete arr[i].auditData;
 delete arr[i].dataLockVersion;
 delete arr[i].domainObjectDescription;
 delete arr[i].id;
 result.push(arr[i]);
}

Is there a better way to do this ?

Luca Kiebel
10.1k7 gold badges34 silver badges47 bronze badges
asked Sep 18, 2018 at 10:53
2
  • I have updated my answer by adding a new method with a Innovative way! you can check it too. Commented Sep 18, 2018 at 11:27
  • You can use restructuring assignment , check my answer Commented Sep 18, 2018 at 12:02

6 Answers 6

3

use map and object destructure

const result = array
 .map(
 ({ property1, property2, property3 })
 => ({ property1, property2, property3 }));
answered Sep 18, 2018 at 11:11
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply use Array.map() and object destructuring for this:

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);

answered Sep 18, 2018 at 10:59

Comments

1

I'd use lodash.pick as a oneliner clean and efficient solution.

Pretty often it turns out that this kind of logic will be needed in other parts of the app.

In your case it would be:

var newArrayWithPickedProperties = array.map(item => {
 return _.pick(item, ['property1', 'property2', 'property3']); 
})

If you go this way ensure you import only lodash.pick not entire lodash library.

answered Sep 18, 2018 at 11:06

Comments

1

you can try this:

 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);


Or if you like tricks and all values are string or number or object that contains them, you can use this (in very heavy objects is not recommended):

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);

answered Sep 18, 2018 at 11:01

2 Comments

I had a similar problem I was trying to solve for like 6 hours, that property assignment with map and function saved my sanity
I am glad it was useful ;)
0

If you have n number of property with fixed 1st 3 keys, you can do destructuring assignment. 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);

answered Sep 18, 2018 at 11:42

Comments

0

If data is from a JSON string, the JSON.parse reviver parameter can be used to exclude properties :

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 )

answered Sep 18, 2018 at 11:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.