1

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 
}
3
  • its let not Let, and what is the value of i ? Commented Apr 18, 2019 at 10:48
  • Just to make this sprint even funnier, do you expect just 'true'/'false' output or index of the matching item(s) within the array or the matching object itself? Are you looking for exact or partial match? Commented Apr 18, 2019 at 11:04
  • All most all the answers here working perfectly. Commented Apr 18, 2019 at 11:13

9 Answers 9

5

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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")}

Comments

0

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

4 Comments

Your are creating a copy of the array just for looking if an item is included... I would directly use Array.prototype.some() or Array.prototype.find() instead.
@jo_va that's fair criticism. But I doubt it matters unless you're going to be using an enormous array (as in thousands of objects). And I find includes and map more readable because it's clearer what you're doing, .some is more generic and you have to look at the details of the function passed to it to figure out what's going on. Sure in this case it's easy but it's the kind of small "mental tax" that is better to avoid.
I also voted for 'some' solution as it seems to me more concise and elegant, but, strangely, 'map+includes' works faster, which doesn't change my opinion, of course.
Interesting, thanks. I'm always a bit sceptical of such tests - but I'm actually not that surprised. I expect JS engines optimise specifically for things like .includes - and the overhead of creating a new array with map, as I said, most likely only becomes noticeable with much bigger arrays.
0

You can use -

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

Comments

0

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

}
}

Comments

0

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.

Comments

0

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'));

2 Comments

...too late :) posted 6 mins ago
But destructuring assignment adds certain charm
0

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;
    }
}

1 Comment

Please describe what did you change and why, to help others identify the problem and understand this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.