Reputation: 6908
Considering follow code
const filterArray = ['a', 'b']
const objectToBeFilter = {
a: {
ab: 'ab',
ac: 'ac',
ad: 'ad'
},
b: {
bb: 'bb',
bc: 'bc',
bd: 'bd'
},
c: {
cb: 'cb',
cc: 'cc',
cd: 'cd'
}
}
const resultWantToGet = [
a: {
ab: 'ab',
ac: 'ac',
ad: 'ad'
},
b: {
bb: 'bb',
bc: 'bc',
bd: 'bd'
}
]
FilterArray
is a array to be used as a filter index, objectToBeFilter
is a object which I want to handle. How to use filterArray to filter objectToBeFilter and then convert it to a array resultWantToGet
?
Upvotes: 0
Views: 53
Reputation: 92440
Based on your last comment, it seem like you want an array of objects. You can do this simply with, which will give you the objects from objectToBeFilter
keyed at a
and b
in an array:
const filterArray = ['a', 'b']
const objectToBeFilter = {
a: {
ab: 'ab',
ac: 'ac',
ad: 'ad'
},
b: {
bb: 'bb',
bc: 'bc',
bd: 'bd'
},
c: {
cb: 'cb',
cc: 'cc',
cd: 'cd'
}
}
let result = filterArray.map(key => objectToBeFilter[key])
console.log(result)
This looks different than your expected result because arrays don't have keys. If you want each item to be identified with a key, you probably want an object, but if you want an ordered collection, then an array is appropriate.
Upvotes: 1
Reputation: 2348
you can use the following code Object.keys(objectToBeFilter)
get list of filtered object keys then filter keys that included in filter array .filter(key => filterArray.includes(key))
then construct a new object with only the filtered properties reduce((obj, key)
const filterArray = ['a', 'b']
const objectToBeFilter = {
a: {
ab: 'ab',
ac: 'ac',
ad: 'ad'
},
b: {
bb: 'bb',
bc: 'bc',
bd: 'bd'
},
c: {
cb: 'cb',
cc: 'cc',
cd: 'cd'
}
}
const resultWantToGet = Object.keys(objectToBeFilter)
.filter(key => filterArray.includes(key))
.reduce((obj, key) => {
obj[key] = objectToBeFilter[key];
return obj;}, {});
console.log(resultWantToGet)
Upvotes: 1
Reputation: 26844
You can use reduce
to loop and construct new object.
const filterArray = ['a', 'b']
const objectToBeFilter = {
a: {
ab: 'ab',
ac: 'ac',
ad: 'ad'
},
b: {
bb: 'bb',
bc: 'bc',
bd: 'bd'
},
c: {
cb: 'cb',
cc: 'cc',
cd: 'cd'
}
}
const resultWantToGet = filterArray.reduce((c, v) => {
if (objectToBeFilter[v]) c[v] = objectToBeFilter[v]; //Check if the key exist in objectToBeFilter, if it does, assign to the accumulator.
return c;
}, {})
console.log(resultWantToGet);
Upvotes: 1
Reputation: 370679
You can map
by filterArray
and spread into a new object:
const filterArray = ['a', 'b']
const objectToBeFilter = {
a: {
ab: 'ab',
ac: 'ac',
ad: 'ad'
},
b: {
bb: 'bb',
bc: 'bc',
bd: 'bd'
},
c: {
cb: 'cb',
cc: 'cc',
cd: 'cd'
}
};
console.log({
...filterArray.map(key => ({ [key]: objectToBeFilter[key] }))
});
Or, if you don't want to create intermediate objects, use reduce
:
const filterArray = ['a', 'b']
const objectToBeFilter = {
a: {
ab: 'ab',
ac: 'ac',
ad: 'ad'
},
b: {
bb: 'bb',
bc: 'bc',
bd: 'bd'
},
c: {
cb: 'cb',
cc: 'cc',
cd: 'cd'
}
};
console.log(filterArray.reduce((a, prop) => {
a[prop] = objectToBeFilter[prop];
return a;
}, {}));
Upvotes: 1