javascript - Need help understanding lodash's _.includes method -


i use lodash's _.includes method in code, time have array of objects can't work, , instead end relying on _.find method.

from tests can _.includes work arrays. maybe that's way it's supposed work?

i new lodash , programming in general, thought ask in case missing how can use method.

i created jsbin following code: http://jsbin.com/regojupiro/2/

var myarray = [];  function createarray(attname, attid, otherid) {     var theobject = {};      theobject.attributename = attname;     theobject.attributeid = attid;     theobject.otherid = [otherid];        return theobject; }  myarray.push(createarray('first', 1001, 301)); myarray.push(createarray('second', 1002, 302)); myarray.push(createarray('third', 1003, 303)); myarray.push(createarray('fourth', 1004, 304));  var ispresent1 = _.includes(myarray, {'attribtueid' : 1001}); var ispresent2 = _.includes(myarray, 1001); var found = _.find(myarray, {'attributeid' : 1001});  console.log(ispresent1); console.log(ispresent2); console.log(found); console.log(myarray); 

both "ispresent" variables return false, _.find method returns correct object.

i love in better understanding how use _.includes method when need simple true/false check see if value present in array of objects.

or, if wrong tool job, _.find method right tool job, or other lodash method i'm not familiar yet?

thank help!

the _.includes() method compares samevaluezero comparator, special comparison ===. if have object in array looks {'attribtueid' : 1001} _.includes() call never find because 2 distinct objects never compare === each other.

when pass object _.find(), contrast, library assumes want carry out _.matches() comparison, compare properties of "target" object. thus, in case, _.find() right choice. _.includes method fills distinct niche.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -