CirLorm
CirLorm

Reputation: 21

How to compare a string with a value of an object in an array

I want to check if a string matches another string in an array of objects.

Here's my code

let myArr = [{title: "fruits"}, {title: "vegetables"}];
//I want to match a string with the 'title' of the objects 

var str = "string";
if ( myArr[i].title == str) {
    //Do something 
}

Upvotes: 1

Views: 2683

Answers (9)

WSD
WSD

Reputation: 3587

First things first. Avoid the capital letter on the Let myVariable is just let myVariable.
Also consider using const instead of let for those variables that aren't going to change :)
Now, to answer your question, you can use the some method. Something like this:

const myArr = [{title: "fruits"}, {title: "vegetables"}];
const str = 'fruits';
console.log('Exist?', myArr.some((obj)=>obj.title===str));
// This will output Exist? true

Upvotes: 5

Sadia
Sadia

Reputation: 105

read each title element of the array using for loop -->

let myArr = [{title: "fruits"}, {title: "vegetables"}];

let str = "string";
for(let i=0; i < myArr.length; i++) {
    if (myArr[i].title === str) {
          return true;
    }
}

Upvotes: 0

jo_va
jo_va

Reputation: 13963

I would use Array.prototype.some() or Array.prototype.find() with !! before to turn the value to a boolean:

const myArr = [ { title: 'fruits' }, { title: 'vegetables' } ];

console.log(myArr.some(({ title }) => title === 'fruits'));
console.log(!!myArr.find(({ title }) => title === 'fruits'));

Upvotes: 0

Arvind Rajput
Arvind Rajput

Reputation: 114

I am using it in my code and working perfectly from me

var fruitsObj = myArr.find(element => element.title == "fruits")

You will get the object which contains title fruits that is {title: "fruits"} in your case.

Upvotes: 0

Besart Marku
Besart Marku

Reputation: 543

Using ES6

   let myArr = [{title: "fruits"}, {title: "vegetables"}];
    
    const checkTitle = obj => obj.title === 'fruits';
    
    //check if it is found
    if((myArr.some(checkTitle))){
    //do your stuff here
    console.log("it exists, yay")}

Upvotes: 1

Arun
Arun

Reputation: 100

You can just loop through the array elements and compare them with the str.

var myArr = [{title: "fruits"}, {title: "vegetables"}];
var str = "string";

for (i=0;i<myArr.length;i++){
if (myArr[i].title===str){
console.log(true);
}
else {
console.log(false);

}
}

Upvotes: 0

Robin Zigmond
Robin Zigmond

Reputation: 18249

Since you're clearly already using ES6, the most idiomatic way is using Array.includes after mapping the array:

let myArr = [{title: "fruits"}, {title: "vegetables"}];

var str = "string";

let match = myArr.map(obj => obj.title).includes(str);

console.log(match);

Upvotes: 0

Al Amin
Al Amin

Reputation: 949

You can use -

let match = false
myArr.forEach(function(element){
    if(element.title === str){
        match = true;
    }
});

Upvotes: 0

Marcelo Vismari
Marcelo Vismari

Reputation: 1179

let myArr = [{ title: "fruits" }, { title: "vegetables" }];
var str = "string";
if (myArr.find(a => a.title == str) != null) {
    console.log('aaa');
}

Upvotes: 1

Related Questions