4

.NET's LINQ contains an operator .Single which

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

Is there an equivalent in the latest version of JavaScript? (ECMA v8 at time of writing).

The closest operation that I could find was .find though one would have to write their own boilerplate to assert that exactly one elemet matched.

Example of expected functionality of .single():

const arr = [1, 4, 9];
arr.single(v => v > 8 && v < 10);   // Returns 9
arr.single(v => v < 5);             // Throws an exception since more than one matches
arr.single(v => v > 10);            // Throws an exception since there are zero matches
1

3 Answers 3

1

Is there an equivalent in the latest version of JavaScript? (ECMA v8 at time of writing).

No, it is not.

You could change the prototype of Array (which is not advisable) and add a method which filters the array and returns a single found item or throws an error.

Array.prototype.single = function (cb) {
    var r = this.filter(cb);
    if (!r.length) {
        throw 'zero match';
    }
    if (r.length !== 1) {
        throw 'too much matches';
    }
    return r[0];
};

const arr = [1, 4, 9];
console.log(arr.single(v => v > 8 && v < 10)); // 9
console.log(arr.single(v => v < 5));           // exception since more than one matches
// console.log(arr.single(v => v > 10));       // exception since there are zero matches

Or you could use link.js.

const arr = [1, 4, 9];
console.log(Enumerable.From(arr).Single(v => v > 8 && v < 10)); // 9
console.log(Enumerable.From(arr).Single(v => v < 5));           // exception since more than one matches
// console.log(Enumerable.From(arr).Single(v => v > 10));       // exception since there are zero matches
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

No such function exists in native JS, but it's trivial to write your own that accomplishes the same thing:

const single = (arr, test) => {
  let found;
  for (let i = 0, l = arr.length; i < l; i++) {
    if (test(arr[i])) {
      if (found !== undefined) throw new Error('multiple matches found');
      found = arr[i];
    }
  }
  if (!found) throw new Error('no matches found');
  console.log('found: ' + found);
  return found;
}

const arr = [1, 4, 9];
single(arr, v => v > 8 && v < 10);   // Returns 9
single(arr, v => v < 5);             // Throws an exception since more than one matches
single(arr, v => v > 10);            // Throws an exception since there are zero matches

That's assuming you're not actually testing for undefined, in which case you'd have to write a bit more code

Comments

0

With https://www.npmjs.com/package/manipula you can do it by LINQ style:

Manipula.from([1, 4, 9]).single(v=> v>8 && v<10)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.