teebeetee
teebeetee

Reputation: 549

Extract certain properties from all objects in array

I have an array of objects with the same properties. Each object has around a hundred properties. I want to keep only a handful of them in a new array:

const dummyArray = [
    { "att1": "something", "att2": "something", /* … */, "att100": "something" },
    { "att1": "something", "att2": "something", /* … */, "att100": "something" },
    // …
  ];

How can I filter, map, reduce the array, or use some other method to extract the desired keys?

const newDummArray = dummyArray.map(function(item) { 
    delete item.att1; 
    delete item.att3; 
    delete item.att15;
    // Long list …

    return item; 
  });

How can I keep only att20, att30, att70, att80 for each object and delete the rest?

Upvotes: 11

Views: 16496

Answers (5)

Abhishek Tomar
Abhishek Tomar

Reputation: 905

I have find easiest way to do this JSON.stringify()

The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

    const odata = [
      { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55 },
      { "id": "0002", "type": "ansd", "name": "EARK", "ppu": 0.67 }
    ];
    const outdata=JSON.stringify(odata,['id','type']);
    console.log(outdata);

Upvotes: 6

Mohamed Allal
Mohamed Allal

Reputation: 20860

Here a function that take an object, and extract only the properties that you want. Which you passe through an array as a second parameter.

Advantage: more straight forward and cleaner. Specially when you need to extract from just one object.

If you have a list of object. Map through the list and extract in every iteration.

function objectExtract(obj, properties) {
    return properties.reduce((result, prop) => {
        if (obj.hasOwnProperty(prop)) {
            result[prop] = obj[prop];
        }
        return result;
    }, {});
}

Read about reduce here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce .

use:

(a real example with redux)

store.dispatch(
            setRooms({ 
                ...roomsState,
                list: rooms.reduce((list, room) => {
                    list[room.id] = objectExtract(room, ['name', 'description', 'createdAt', 'updatedAt']);
                    return list;
                }, {})
            })
        )

(On the example of the question)

var dataSourceArray = [{
  "att1": "something",
  "att2": "something",
  "att20": "something",
  "att100": "something"
}, {
  "att1": "something",
  "att2": "something",
  "att20": "something",

  "att100": "something"
}];

let x = dataSourceArray.map((item) => {
  return objectExtrac(item, ['att100', 'att2']);
});

Upvotes: 2

Ori Drori
Ori Drori

Reputation: 191966

Use object destructuring to get the properties, and generate a new object using shorthand property names:

const dummyArray = [{ "att20": "att20", "att30": "att30", "att70": "att70", "att80": "att80"}, { "att20": "att20", "att30": "att30", "att70": "att70", "att80": "att80"}];

const result = dummyArray.map(({ att20, att30, att70, att80 }) => ({
  att20, 
  att30, 
  att70, 
  att80
}));

console.log(result);

Upvotes: 21

marzelin
marzelin

Reputation: 11600

store the props you want to keep in an array then for each object transfer wanted props to a new object.

var dummyArray = [{ "att1": "something", "att2": "something", "att100": "something"}, { "att1": "something", "att2": "something", "att100": "something"}];

var propsToKeep = ["att1", "att100"];

var result = dummyArray.map(item => {
  const obj = {};
  for (const prop of propsToKeep) {
    obj[prop] = item[prop];
  }
  return obj;
})

console.log(result)

Upvotes: 8

brk
brk

Reputation: 50291

map creates a new array, so there is no need to delete any thing, instead create an array of interesting keys and return it

var dummyArray = [{
  "att1": "something",
  "att2": "something",
  "att20": "something",
  "att100": "something"
}, {
  "att1": "something",
  "att2": "something",
  "att20": "something",

  "att100": "something"
}];

let x = dummyArray.map((item) => {
  return {
    attr20: item.att20
  }

})

console.log(x)

Upvotes: 8

Related Questions