DEV Community

PVRS
PVRS

Posted on

How to JSON Object value without using Map

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)

Collapse
 
suryakandikonda profile image
Surya Kandikonda • Edited

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...

Collapse
 
rs_pv_92 profile image
PVRS • Edited

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...

Collapse
 
suryakandikonda profile image
Surya Kandikonda

Image is not uploaded in comment..

Thread Thread
 
rs_pv_92 profile image
PVRS
Thread Thread
 
suryakandikonda profile image
Surya Kandikonda

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

Thread Thread
 
rs_pv_92 profile image
PVRS

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"]);