We have an array of objects like
const arr = [{id: "someId", name: {...}}, ...];
const skippedKeys = ["id"...]
How can i filtered the array of object based on skipped keys? The result should be:
const result = [{name: {...}}, ...];
Also i don't want to make a cycle inside the cycle. the result also could be implemented using lodash library. we should remove key with value as well.
It's simple and no need for any nested cycles. There are two option to do that
includes
function const result = arr.filter((item) => !result.includes(item.id));
set
const dataSet = new Set(skippedKeys);
const result = arr.filter((item) => !dataSet.has(item.id));
I prefer the second one as it excludes double checks. Hope the answer was helpful.
const result = arr.map(obj =>
Object.keys(obj).reduce(
(res, key) => (
skippedKeys.includes(key) ? res : {...res, [key]: obj[key]}
),
{},
));
Since you stated that it could be implemented using lodash... here code using lodash
let result = _.map(arr, (el)=> _.omit(el, skippedKeys))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.