12

In the below, function redux1 removes entries corresponding to keys not listed in keys_to_keep from data object.

Given that I have a list of object keys to keep, how can I rewrite redux1 in a cleaner way, prefarably utilizing map, filter or reduce?

var data = [
    {name: 'John', city: 'London', age: 42},
    {name: 'Mike', city: 'Warsaw', age: 18},
    {name: 'Jim', city: 'New York', age: 22},
    {name: 'Celine', city: 'Tokyo', age: 54},
]

var keys_to_keep = ['name', 'city']

function redux1(data) {
    data.forEach((person) => {
        Object.keys(person).forEach((key) => {
            if (!keys_to_keep.includes(key)) {
                delete (person[key])
            }
        })
    })
    console.log(data)
}

function redux2(data) { 
    var reduced = data.filter(person => Object.keys(person).filter(key => keys_to_keep.includes(key)))
    console.log(reduced)
}

redux1(data)
//redux2(data)

My current redux2 will return objects will not remove age.

4
  • "...in a cleaner way" - what's not "clean" about your function? "...prefarably utilizing map, filter or reduce?" - Why those three? Commented Feb 27, 2019 at 14:25
  • @Heretic Monkey, not a duplicate. I know this answer, but it only covers deleting specific key, not all keys not found in an array. Commented Feb 27, 2019 at 14:53
  • Then you should mention that in your question. stackoverflow.com/help/duplicates Commented Feb 27, 2019 at 14:54
  • Closely related: Extract certain properties from all objects in array. Commented Nov 3, 2022 at 21:51

6 Answers 6

29

You could use a combination of Array#map and Array#reduce:

const data = [
	{name: 'John', city: 'London', age: 42},
	{name: 'Mike', city: 'Warsaw', age: 18},
	{name: 'Jim', city: 'New York', age: 22},
	{name: 'Celine', city: 'Tokyo', age: 54},
]

const keys_to_keep = ['name', 'city'];

const redux = array => array.map(o => keys_to_keep.reduce((acc, curr) => {
  acc[curr] = o[curr];
  return acc;
}, {}));

console.log(redux(data));

Sign up to request clarification or add additional context in comments.

Comments

9

A version slightly shorter than the accepted answer using Array#map and Object.fromEntries():

const data = [
    {name: 'John', city: 'London', age: 42},
    {name: 'Mike', city: 'Warsaw', age: 18},
    {name: 'Jim', city: 'New York', age: 22},
    {name: 'Celine', city: 'Tokyo', age: 54},
]

const keys_to_keep = ['name', 'city'];

const redux1 = list => list.map(o => Object.fromEntries(
    keys_to_keep.map(k => [k, o[k]])
));

console.log(redux1(data));

3 Comments

Thanks. Useful :) I need also an exclusion. Pasted from your code with field excluded : data.map(o => Object.fromEntries( Object.keys(o).filter((n) => ["age"].indexOf(n) < 0 ).map(k => [k, o[k]]) ))
@phili_b You could directly filter the entries instead of adding another map: Object.fromEntries( Object.entries(o).filter(([k]) => !["age"].includes(k) ) )
Ha yes, you're right :)
3

use Array.map and Array.forEach inside it :

var data = [
	{name: 'John', city: 'London', age: 42},
	{name: 'Mike', city: 'Warsaw', age: 18},
	{name: 'Jim', city: 'New York', age: 22},
	{name: 'Celine', city: 'Tokyo', age: 54},
]

var keys_to_keep = ['name', 'city']

const result = data.map(e => {
  const obj = {};
  keys_to_keep.forEach(k => obj[k] = e[k])
  return obj;
});

console.log(result);

Comments

2

var data = [
	{name: 'John', city: 'London', age: 42},
	{name: 'Mike', city: 'Warsaw', age: 18},
	{name: 'Jim', city: 'New York', age: 22},
	{name: 'Celine', city: 'Tokyo', age: 54},
]

var keys_to_keep = ['name', 'city']

data=data.map(element => Object.assign({}, ...keys_to_keep.map(key => ({[key]: element[key]}))))

console.log(data)

Comments

0
data.reduce((r, c) => [ ...r, Object.entries(c).reduce((b, [k, v]) => keys_to_keep.includes(k) ? {...b, [k]: v } : b, {}) ],[])

Comments

0

You can use Object.entries

function objfilter(data,keys_to_keep){
    return Object.fromEntries(Object.entries(data).filter(a=>keys_to_keep.includes(a[0])))
}
const data = [
    {name: 'John', city: 'London', age: 42},
    {name: 'Mike', city: 'Warsaw', age: 18},
    {name: 'Jim', city: 'New York', age: 22},
    {name: 'Celine', city: 'Tokyo', age: 54},
]

const keys_to_keep = ['name', 'city'];

console.log(objfilter(data,keys_to_keep))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.