Renaud is Not Bill Gates
Renaud is Not Bill Gates

Reputation: 2084

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 ?

Upvotes: 4

Views: 151

Answers (6)

Slai
Slai

Reputation: 22866

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 )

Upvotes: 0

sumit
sumit

Reputation: 15464

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

Upvotes: 0

Hassan Sadeghi
Hassan Sadeghi

Reputation: 1326

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

Upvotes: 1

klimat
klimat

Reputation: 24991

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.

Upvotes: 1

Waqas Noor
Waqas Noor

Reputation: 921

use map and object destructure

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

Upvotes: 3

amrender singh
amrender singh

Reputation: 8239

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

Upvotes: 1

Related Questions