+ 1
How to get position of array data
I have two arrays var item = [{"title":"how to cook food","description":"",{"title":"............]; var words = ["food","cook"]; Now I want each position of item array whose title contains atleast one of words value. I tried this one var data = []; items.forEach(function(element,index,array){ for(var x = 0; x < words.length; x++){ if(element[index].title.includes(words[x])){ data.push(element[index].title); } } }); But it gives can't read property of title, include is not property and also I tried loop inside loop but it hang. Please help me.
2 Answers
+ 6
var items = [
{
"title":"how to cook food",
"description":"use the hands",
},
{
"title":"how to eat food",
"description": "use the mouth"
},
{
"title":"how to drink water",
"description": "use the mouth"
},
];
var words = ["food","cook"];
var data = [];
items.forEach( function(element,index,array) {
for(var x = 0; x < words.length; x++){
if(element.title.includes(words[x])){
data.push(element.title);
break;
}
}
});
+ 1
CalviŐ˛ Thanks