Javascript 如何检查数组是否包含 TypeScript 中的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42790602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:25:58  来源:igfitidea点击:

How do I check whether an array contains a string in TypeScript?

javascriptarraystypescript

提问by code1

Currently I am using Angular 2.0. I have an array as follows:

目前我正在使用 Angular 2.0。我有一个数组如下:

var channelArray: Array<string> = ['one', 'two', 'three'];

How can I check in TypeScript whether the channelArray contains a string 'three'?

如何在 TypeScript 中检查 channelArray 是否包含字符串“three”?

回答by baao

The same as in JavaScript, using Array.prototype.indexOf():

和 JavaScript 一样,使用Array.prototype.indexOf()

console.log(channelArray.indexOf('three') > -1);

Or using ECMAScript 2016 Array.prototype.includes():

或者使用 ECMAScript 2016 Array.prototype.includes()

console.log(channelArray.includes('three'));


Note that you could also use methods like showed by @Nitzan to find a string. However you wouldn't usually do that for a string array, but rather for an array of objects. There those methods were more sensible. For example

请注意,您还可以使用@Nitzan 所示的方法来查找字符串。但是,您通常不会对字符串数组执行此操作,而是针对对象数组执行此操作。那些方法更明智。例如

const arr = [{foo: 'bar'}, {foo: 'bar'}, {foo: 'baz'}];
console.log(arr.find(e => e.foo === 'bar')); // {foo: 'bar'} (first match)
console.log(arr.some(e => e.foo === 'bar')); // true
console.log(arr.filter(e => e.foo === 'bar')); // [{foo: 'bar'}, {foo: 'bar'}]

Reference

参考

Array.find()

数组查找()

Array.some()

数组.some()

Array.filter()

Array.filter()

回答by Nitzan Tomer

You can use the some method:

您可以使用一些方法

console.log(channelArray.some(x => x === "three")); // true

You can use the find method:

您可以使用查找方法

console.log(channelArray.find(x => x === "three")); // three

Or you can use the indexOf method:

或者您可以使用indexOf 方法

console.log(channelArray.indexOf("three")); // 2

回答by alejoko

If your code is ES7 based:

如果您的代码基于 ES7:

channelArray.includes('three'); //will return true or false

If not, for example you are using IE with no babel transpile:

如果没有,例如您使用的是没有 babel transpile 的 IE:

channelArray.indexOf('three') !== -1; //will return true or false

the indexOfmethod will return the position the element has into the array, because of that we use !==different from -1 if the needle is found at the first position.

indexOf方法将返回元素在数组中的位置,因为!==如果在第一个位置找到针,我们使用与 -1 不同的位置。

回答by David Dehghan

Also note that "in" keyworddoes not work on arrays. It works on objects only.

另请注意,“in”关键字不适用于数组。它仅适用于对象。

propName in myObject

Array inclusion test is

数组包含测试是

myArray.includes('three');

回答by Basi

Use JavaScript Array includes()Method

使用JavaScript Array includes()方法

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango");

Try it Yourself ? link

自己试试?关联

Definition

定义

The includes()method determines whether an array contains a specified element.

包括()方法确定阵列是否包含指定的元素。

This method returns true if the array contains the element, and false if not.

如果数组包含元素,则此方法返回 true,否则返回 false。

回答by Willem van der Veen

TS has many utility methods for arrays which are available via the prototype of Arrays. There are multiple which can achieve this goal but the two most convenient for this purpose are:

TS 有许多用于数组的实用方法,可通过 Arrays 的原型获得。有多种方法可以实现这一目标,但最方便的两种方法是:

  1. Array.indexOf()Takes any value as an argument and then returns the first index at which a given element can be found in the array, or -1 if it is not present.
  2. Array.includes()Takes any value as an argument and then determines whether an array includes a this value. The method returning trueif the value is found, otherwise false.
  1. Array.indexOf()将任何值作为参数,然后返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回 -1。
  2. Array.includes()将任何值作为参数,然后确定数组是否包含 this 值。true如果找到该值则返回该方法,否则返回false.

Example:

例子:

var channelArray: string[] = ['one', 'two', 'three'];
console.log(channelArray.indexOf('three'));      // 2
console.log(channelArray.indexOf('three') > -1); // true
console.log(channelArray.indexOf('four') > -1);  // false
console.log(channelArray.includes('three'));     // ture

回答by Arvind Chourasiya

You can use filtertoo

您可以使用filter

this.products = array_products.filter((x) => x.Name.includes("ABC"))

回答by Abdus Salam Azad

do like this:

这样做:

departments: string[]=[];
if(this.departments.indexOf(this.departmentName.trim()) >-1 ){
            return;
    }