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
}
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
}
Share
Improve this question
edited Apr 18, 2019 at 10:52
CirLorm
asked Apr 18, 2019 at 10:46
CirLormCirLorm
211 silver badge9 bronze badges
3
-
its
let
notLet
, and what is the value ofi
? – Durga 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? – Yevhen Horbunkov Commented Apr 18, 2019 at 11:04
- All most all the answers here working perfectly. – CirLorm Commented Apr 18, 2019 at 11:13
9 Answers
Reset to default 5First 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
let myArr = [{ title: "fruits" }, { title: "vegetables" }];
var str = "string";
if (myArr.find(a => a.title == str) != null) {
console.log('aaa');
}
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")}
Since you're clearly already using ES6, the most idiomatic way is using Array.includes
after map
ping the array:
let myArr = [{title: "fruits"}, {title: "vegetables"}];
var str = "string";
let match = myArr.map(obj => obj.title).includes(str);
console.log(match);
You can use -
let match = false
myArr.forEach(function(element){
if(element.title === str){
match = true;
}
});
You can just loop through the array elements and pare 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);
}
}
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.
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'));
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;
}
}