-7

I have an array within an angular scope that basically contains an object within each iteration. I have used the indexOf() function before to check if a value exists within an array - but how can I check if a value exists within the object word value?

I want to use a foreach loop and just check if a word exists within the rules array in the format shown below - how is the best way to achieve this?

rules = [];

rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}
Zabs
  • 13,852
  • 45
  • 173
  • 297
  • 1
    `Object.values(o).some(v => v === needle)`? – Ben Aston Dec 06 '16 at 10:00
  • 4
    [Please search before posting](/search?q=%5Bjs%5D+search+array+of+objects), there are dozens of questions (with hundreds of answers) on the topic, your question is answered by at least one of them and probably several. More on searching [here](/help/searching). – T.J. Crowder Dec 06 '16 at 10:02
  • 3
    Possible duplicate of [How do I check if an array includes an object in JavaScript?](http://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Dvid Silva Dec 06 '16 at 10:09

4 Answers4

1
for(var i =0;i < rules.length;i++){
  if(rules[i].word === 'valueYouWantToCheck'){
   // do whatever you want to do
  }
}

Try this...:)

Dhaval Chaudhary
  • 5,428
  • 2
  • 24
  • 39
  • He mentioned in question, he want to use ```forEach``` loop. – bharadhwaj Dec 06 '16 at 10:07
  • @bharadhwaj foreach will serve same. just he has to pass index and item like rules.forEach(someFunction) and write function like someFunction(index,item){} – Dhaval Chaudhary Dec 06 '16 at 10:10
  • This is a working solution, I agree! But he explicitly mentioned he **WANTS** to use a ```forEach``` loop. That is the reason I notified you! Also he just wants to know whether such a key ```word``` exist, not comparing it with some value, I hope! Otherwise, it is a good solution! – bharadhwaj Dec 06 '16 at 10:19
  • @bharadhwaj yes got your concern.thank you for pointing that out.i will take care such things next time...:D – Dhaval Chaudhary Dec 06 '16 at 14:43
  • No offense brother! Just pointed out as I noted! :) – bharadhwaj Dec 07 '16 at 06:07
1

rules = []

rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}
rules[3] = { sentence:"horse"}

rules.forEach(rule => {
    if (rule.word) {
      console.log('Exist. Value:', rule.word)
    } else {
      console.log('Doesn\'t Exist.')
    }
})

Hope this helps!

bharadhwaj
  • 2,059
  • 22
  • 35
1

You can use this

var rules = [];
rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}
rules.forEach(function(item){
   console.log(item.word)  // your word of choice
})

You can also use filter function. If your required word is matched it will return the object or else it will return an empty array

var getValue = rules.filter(function(item){
    return item.word=='house';
})
 console.log(getValue)

Beside you can also use .find method

rules.find(function(item){
  return item.word=="house";
})
brk
  • 48,835
  • 10
  • 56
  • 78
-1
rules = [];

rules[0] = { word:"house"}
rules[1] = { word:"shoes"}
rules[2] = { word:"tools"}

for(var i=0; i<rules.length ; i++)
{
   rules[i]["word"]=='valueforComparison'; // or rules[i].word=='valueforComparison';
}
Vibhesh Kaul
  • 2,593
  • 1
  • 21
  • 38