javascript extract certain properties from all obj

2020-01-25 11:47发布

问题:

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:

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

How can I filter/map/reduce... and extract the interesting 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?

回答1:

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



回答2:

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)



回答3:

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)



回答4:

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']);
});