Linq.js returns (default) FirstOrDefault

Trying to check the result of linq.js FirstOrDefault (), but checking for null or undefined does not work. Having some debugging problems, but I see that it returns some kind of object.

There is no documentation on this method on the Internet that I could find.

I tried:

var value = Enumerable.From(stuff).FirstOrDefault('x => x.Name == "Doesnt exist"')

if (value) {
    alert("Should be not found, but still fires");
}

if (value != null)
    alert("Should be not found, but still fires");
}
+4
source share
2 answers

Signatures for the function FirstOrDefault():

// Overload:function(defaultValue)
// Overload:function(defaultValue,predicate)

The first parameter is always the default to return if the collection is empty. The second parameter is the predicate for the search. Your use of the method is incorrect, your request should be written as:

var value = Enumerable.From(stuff)
    .FirstOrDefault(null, "$.Name === 'Doesnt exist'");
+9
source

, . , .

Where FirstOrDefault().

var someArray = ["Foo", "Bar"];
var result = Enumerable.From(someArray).Where('x => x == "Doesnt exist"').FirstOrDefault();

undefined ()

var someArray = ["Foo", "Bar"];
var result = Enumerable.From(someArray).Where('x => x == "Bar"').FirstOrDefault();

- "" ()

var someArray = ["Foo", "Bar"];
var result = Enumerable.From(someArray).FirstOrDefault('x => x == "Bar"');

- "Foo" ()

+3

All Articles