Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help understanding lodash's _.includes method

I would like to use lodash's _.includes method in my code, but any time I have an array of objects I can't get it to work, and instead end up relying on the _.find method.

From my tests I can only get _.includes to work with simply arrays. But maybe that's the way it's supposed to work?

I am very new to Lodash and programming in general, so I thought I would ask in case I am missing something about how I can use this method.

I created a jsbin with the 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, but the _.find method returns the correct object.

I would love some help in better understanding how I could use the _.includes method when I just need to do a simple true/false check to see if a value is present in my array of objects.

Or, if this is the wrong tool for the job, is the _.find method the right tool for this job, or some other lodash method that I'm not familiar with yet?

Thank you for your help!

like image 998
Zigrivers Avatar asked Mar 31 '15 17:03

Zigrivers


People also ask

What is_ includes?

The _. includes() method is used to find the value is in the collection or not. If the collection is a string, it will be tested for a value sub-string, otherwise SameValueZero() method is used for equality comparisons.

Is array equal Lodash?

The Lodash _. isEqual() Method performs a deep comparison between two values to determine if they are equivalent. This method supports comparing arrays, array buffers, boolean, date objects, maps, numbers, objects, regex, sets, strings, symbols, and typed arrays.

Why use lodash?

Lodash helps programmers write more concise and easier to maintain JavaScript code. Lodash contains tools to simplify programming with strings, numbers, arrays, functions and objects. By convention, Lodash module is mapped to the underscore character.


2 Answers

I think some() does exactly what you're looking for.

like image 116
Adam Boduch Avatar answered Oct 17 '22 23:10

Adam Boduch


The _.includes() method compares with the SameValueZero comparator, which is a special comparison mostly like ===. Even if you have an object in your array that looks like {'attribtueId' : 1001} that _.includes() call will never find it because two distinct objects will never compare as === to each other.

When you pass an object to _.find(), by contrast, the library assumes that you want it to carry out an _.matches() comparison, which will compare properties of the "target" object. Thus, in your case, _.find() is probably the right choice. The _.includes method really fills a distinct niche.

like image 37
Pointy Avatar answered Oct 17 '22 23:10

Pointy