First of all, I'm sorry that this might be a duplicate of another question somewhere, but I don't know if my wording was wrong in my searches, but I couldn't find an exact solution to the issue I'm having, so I do apologize in advance if this is a duplicate.
Now the issue.
I have an array of objects, that has various properties. I then have another array of strings, where each string represents the name of a property on the objects from the object array.
I want to create a new array of objects, but where the objects in the array, only have the properties with names from the string array.
I have been messing around with this one for a couple of hours, but haven't really got it to work as intended. I've tried doing some Object.keys stuff, combined with both map and indexOf, but I feel like I'm missing something in my logic, and at this point I'm just not sure what.
So basically I have an array of objects:
data = [
{
id: 1,
name: 'john',
value: 5
},
{
id: 2,
name: 'ben',
value: 10
},
{
id: 3,
name: 'lisa',
value: 12
},
];
And an array of strings:
desiredKeys = ['id', 'name'];
And I want to create a new array of objects, where each objects only has the 'id' and 'name' property.
Here's a plunker:
https://next.plnkr.co/edit/nU4Y85j7NFuFQqAx?open=lib%2Fapp.ts
My goal would be to create a new array of objects, but where the objects only have the properties in the 'desiredKeys'array, ie:
[
{
id: 1,
name: 'john'
},
{
id: 2,
name: 'ben'
},
{
id: 3,
name: 'lisa'
},
];
Any tips are welcome, thanks in advance!