40

TypeScript simplest way to check if item exists in array like C# Linq Any ( using any library ). Something like

var myArray=[1,2,3,4];

var is_3_Exist=myArray.Any(x=> x==3);

1

6 Answers 6

60

Use .some:

myArray.some(x=>x==3);

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

3 Comments

Sorry, myArray.some(x=>x==3); is not supported in old browser
6

FIDDLE: https://jsfiddle.net/vktawbzg/

NPM: https://www.npmjs.com/package/linqscript

GITHUB: https://github.com/sevensc/linqscript

Take a look at my Github Repository. It would be great if you guys can help to improve it! https://github.com/sevensc/linqscript

Syntax:

list.Any(c => c.Name === ("Apple"))

3 Comments

Could you please tell me how to link your linqscript to already existing TS files? And what should I do to call this stuff from my code?
i made a npm package, get it there: npm install linqscript npmjs.com/package/linqscript You need to import the namespace. See the Readme for more infos.
Ok, I saw the package but don't quite understand how to use it from my TS code. Could you please drop a line of details?
2

You can use the findindex method :

if( myArray.findIndex(x => x === 3) >= 0) {
    // foud myArray element equals to 3
}

Comments

1

If this is the only thing you need to do you should go for .some (with polyfill) if you however want Linq functionality for other things as well you should take a look at https://github.com/ReactiveX/IxJS.

3 Comments

Its nuget package is not working. I found <a href="nuget.org/packages/TypeScriptLinq">TypeScriptLinq</a> working.
github.com/Reactive-Extensions/IxJS should work, it is a repo maintained by microsoft.
What is its Nuget link ?
1

There is a TypeScript library for LINQ.

It is called ts-generic-collections-linq.

Providing strongly-typed, queryable collections such as:

  • List
  • Dictionary

Easy to use.

NPM

https://www.npmjs.com/package/ts-generic-collections-linq

Sample linq query:

let myArray=[1,2,3,4];

let list = new List(myArray);

let is_3_Exist = list.any(x => x==3);

Comments

0

For those who want to fiddle more with prototype to get started writing such 'extension methods':

   if (!Array.prototype.Any) {
  Array.prototype.Any = function <T>(condition: predicate<T>): boolean {
    if (this.length === 0)
      return false;
    let result: boolean = false;
    for (let index = 0; index < this.length; index++) {
      const element = this[index];
      if (condition(element)) {
        result = true;
        break;
      }
    }
    return result;
  }
}

The prototype allows you to add functionality to arrays like and call the functionality easy:

let anyEven = [5, 3, 1, 7, 3, 4, 7].Any(x => x % 2== 0); //true

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.