Eg:
const settingsIcons = [
{ id: 1, icon: "faUserCog", className: "fa-rotate-0" },
{ id: 2, icon: "faUserCog", className: "fa-rotate-360" },
];
In the props, i've id:1 already.. So, i can filter the 1st Object
{ id: 1, icon: "faUserCog", className: "fa-rotate-0" },
but not able you pick : icon: "faUserCog"
Can you suggest, how to read this value : icon: "faUserCog"
Top comments (6)
If you have the object in a variable, say, a = { id: 1, icon: "faUserCog", className: "fa-rotate-0" }.
Then you can get value of the key "icon" by, a["icon"]
Image: dev-to-uploads.s3.amazonaws.com/up...
I tried this, somehow it is not working in React.
It shows Undefined for me, refer the image
dev-to-uploads.s3.amazonaws.com/up...
Image is not uploaded in comment..
dev-to-uploads.s3.amazonaws.com/up...
It is because filter method returns array.. so, iterate over the array in const ic. And get the value of key like I mentioned in my first comment
My bad. Thank you.
Issue Resolved. i changed the logic to use the lodash instead of filter.
const settingsIcons = [
{ id: 1, icon: "faUserCog", className: "fa-rotate-0" },
{ id: 2, icon: "faUserCog", className: "fa-rotate-360" },
];
const ic = _.find(settingsIcons, function (o) {
return (o.id = 1);
});
console.log(ic["icon"]);