Reputation: 131
I have searched this question on google but doesnt find any suitable solution.
Suppose i have an array of objects as below --
"points":[{"pt":"Point-1","value":"Java, j2ee developer"},{"pt":"Point-2","value":"Experienced in Core Java, Spring, Hibernate, JPA, Big Data, SOA, BPEL"}]
Now from above array i want to extract key as wellas values for a specific pair so that my result will look as below --
[{"value":"Java, j2ee developer"},{"value":"Experienced in Core Java, Spring, Hibernate, JPA, Big Data, SOA, BPEL"}]
I know this can be done using manual looping. But i dont wanna loop through. Is it possible to get result in one go using lodash or some other api ?
Upvotes: 0
Views: 122
Reputation: 29989
If you're using the latest version of Javascript you can also make use of destructuring in your call to map, allowing you to extract the properties you care about in an elegant way.
points.map(({ value }) => ({ value }));
The arrow function takes a point
object as an argument and uses { value }
to destructure the point.value
property into a variable called value
.
Then it returns a shorthand object literal which uses value
as both the key and the value.
When compiled with Babel, we get:
points.map(function (_ref) {
var value = _ref.value;
return { value: value };
});
Upvotes: 1