i have an object like this (this is how we see the object in chrome dev tools for example):
obj: {
1: {...},
2: {...},
3: {...},
4: {...},
5: {...},
}
And i have a simple array like this:
arr: [1,3,5,7]
Basically i want my object to stay just with the keys that are in the array, like this:
obj: {
1: {...},
3: {...},
5: {...},
}
At the moment my code is this:
var select = (arr, obj) => arr.reduce((r, e) =>
Object.assign(r, obj[e] ? { [e]: obj[e] } : null)
, {});
var output = select(arr, obj);
I dont know why but this sometimes works and other times don´t. I can not use Jquery. Can anyone help me?
enter code here? And when does it not work? you have example data? the code looks goodobj[e] ?is not a proper check (cause falsy values might not be what you want to check for).e in obj ?would be more accurate.