3个数组中的哪个数组包含一个特定的值,然后使用该数组?Javascript
Which array among 3 arrays contain a specific value and then use that array? Javascript
我有三个数组:
northamerica = ["USA","Canada","Mexico"];
europe = ["France", "Germany", "Italy"];
africa = ["Morocco", "Ghana", "Madagascar"];
我想找出这3个数组中哪一个包含特定的值。它应该是这样的:
输入:
Madagascar
:
The countries in the same continent are: Morocco, Ghana, Madagascar
我怎么能得到这个结果与Javascript?
您可以这样做;
function getContinentNeighbours(country){
return country +
" is in the same continent with: " +
continents.reduce((p,continent) => continent.includes(country) ? p += continent.filter(f => f !== country)
.reduce((f,s) => f + ", " + s)
: p ,"");
}
var northamerica = ["USA","Canada","Mexico"],
europe = ["France", "Germany", "Italy"],
africa = ["Morocco", "Ghana", "Madagascar"],
continents = [northamerica, europe, africa];
console.log(getContinentNeighbours("Mexico"));
您要找的是Array.prototype.includes
。
europe.includes("Madagascar") // false
northamerica.includes("Madagascar") // false
africa.includes("Madagascar") // true
或者您可以使用Array.prototype.indexOf
-如果该元素不在数组中,则返回-1。
相关文章:
- 将值输入到对象,然后该对象推送到数组
- 删除重复的数组值,然后存储它们[反应]
- 如何放置ÅÄ和#214;在javascript数组中,然后将其与html文本进行比较
- 找到 JSON 数组的特定部分,然后在 Angular 中更深入
- 从异步调用返回数组,然后为数组的每个元素返回其他异步调用
- Javascript:编写一个函数,接收一个数组,然后返回一个只有唯一数字的数组,只删除数组
- 查找文档,然后在数组中查找条目并将其从数组中删除
- 如何在一维数组中存储 3 个随机数,然后在 Javascript 中收到警报
- Javascript(动态)插入到数组中,然后移动+1下面的所有元素
- 如何在javascript中生成数组对象的数组,然后将其传递给java
- 从数组中快速打印对象,然后循环
- 根据属性值创建多个对象数组,然后遍历每个数组
- 从数组中删除元素,然后保留其状态以备将来使用,而不使用全局变量
- 量角器:如何在数组中存储值,然后进行排序
- 修改输入中的数组,然后返回输出
- 使用$.post()将PHP数组发送到jQuery,然后循环使用这些值
- 单击时将值推送到数组,然后循环添加数组值.Javascript
- 从下拉列表中的数组中选取某个项目,然后将其显示为第一项并按字母顺序显示
- 如何将字符串拆分为数组,修改数组然后应用于javascript中的字符串
- 如果我们使用new array (len).fil({})创建对象数组;然后在任意一个对象中添加一个键,它会被反射到所有