Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS console log value in array of object attributes

I’m trying to log to console whether an array of objects contains a specific value on a specific attribute of one of these such objects.

in Python this is as simple as

print(val in [x['attribute'] for x in objects]) 

In JS, I think the inclusion operation should function as

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

objects.map(x=>x.attribute).includes(val);
// console.log(objects.map(x=>x.attribute).includes(val).toString());

From reading, I’ve come to understand that raw booleans cannot be logged so they must be converted to string first.

the above is neither logging "true" or "false". So am I doing something wrong? 

>Solution :

Your code is correct if you want to log whether an array of objects contains a specific value on a specific attribute of one of these such objects.

Whether the object contains a value returns a boolean.

Are you trying to log the objects that have an attribute that contain a specific value? If so, I would use filter in stead of map, like this:

objects.filter(x => x. attribute.includes(val)).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading