简体   繁体   中英

Create an array of objects from another array of objects

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 ?

use map and object destructure

const result = array
                   .map(
                       ({ property1, property2, property3 })
                       => ({ property1, property2, property3 }));

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

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.

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

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 ) 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM