如何从另一个数组的值的数组JavaScript编程在最高的索引值 [英] How to get value at highest index in a array from the values of another array with javascript programming

查看:142
本文介绍了如何从另一个数组的值的数组JavaScript编程在最高的索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2组阵

  VAR A = [A,B,C,D,E,F,G,H,我,J,K,L];
变种B = [B,J,A,C,G];

现在我必须从 VAR乙这是最高的指数 VAR A 的值,它是Ĵ

我在变种B到找出Ĵ 通过JavaScript编程

在此先感谢


解决方案

排序第二个数组按降序排列,然后通过迭代找到的元素。您可以使用 排序() 作为排序从大到小的顺序和 找到() 获取的元素。

\r
\r

VAR A = [A,B,C, D,E,F,G,H,I,J,K,L],\r
  B = [B,J,A,C,G];\r
\r
功能customArrayFunction(A,B){\r
  //创建用于存储排序的数组的附加数组变量,否则会更新阵列B的顺序\r
  变种sortB = [];\r
  B.forEach(函数(五){\r
    sortB.push(五);\r
  });\r
\r
  //在按降序排序数组\r
  //而不是定制的比较功能\r
  //你可以使用`sortB.sort()。反向()`简单\r
  VAR解析度= sortB.sort(功能(A,B){\r
    如果(A> B)\r
      返回-1;\r
    否则,如果(A< B)\r
      返回1;\r
    返回0;\r
  })\r
  //寻找元素\r
  .find(函数(五){\r
    返回A.indexOf(ⅴ)> -1;\r
  });\r
  \r
  返回水库;\r
}\r
的console.log(customArrayFunction(A,B));\r
的console.log(customArrayFunction([A,D,G],[B,D,G));

\r

\r
\r


使用ES6箭头功能

\r
\r

VAR A = [A,B,C, D,E,F,G,H,I,J,K,L],\r
  B = [B,J,A,C,G];\r
\r
功能customArrayFunction(A,B){\r
  变种sortB = [];\r
  B.forEach(V => sortB.push(ⅴ));\r
  VAR解析度= sortB.sort((A,B)=> {\r
    如果(A> B)\r
      返回-1;\r
    否则,如果(A< B)\r
      返回1;\r
    返回0;\r
  })找到(V => A.indexOf(v)中。-1个);\r
\r
  返回水库;\r
}\r
的console.log(customArrayFunction(A,B));\r
的console.log(customArrayFunction([A,D,G],[B,D,G));

\r

\r
\r


而无需使用任何自定义比较函数与 <帮助更多的最简单方法href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse\"相对=nofollow> 反向() ,这有助于扭转阵列。

\r
\r

VAR A = [A,B,C, D,E,F,G,H,I,J,K,L],\r
  B = [B,J,A,C,G];\r
\r
功能customArrayFunction(A,B){\r
  变种sortB = [];\r
  B.forEach(V =&GT; sortB.push(ⅴ));\r
  VAR解析度= sortB.sort()\r
    。相反()\r
    .find(V =&GT; A.indexOf(ⅴ)-1个);\r
\r
  返回水库;\r
}\r
的console.log(customArrayFunction(A,B));\r
的console.log(customArrayFunction([A,D,G],[B,D,G));

\r

\r
\r


更新:如果您想根据索引进行搜索,那么就没有必要那种,只是使用 找到()

\r
\r

VAR A = [A,B,C, D,E,F,G,H,I,J,K,L],\r
  B = [B,J,A,C,G];\r
\r
功能customArrayFunction(A,B){\r
  VAR RES = A\r
    .find(V =&GT; B.indexOf(ⅴ)-1个);\r
  返回水库;\r
}\r
的console.log(customArrayFunction(A,B));\r
的console.log(customArrayFunction([A,D,G],[B,D,G));

\r

\r
\r

I have 2 set of array

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
var B = ["B", "J", "A", "C", "G"];

now I have to a value from var B which is at highest index in var A, it is J

I have to find out J in var B by JavaScript programming

Thanks in advance

解决方案

Sort the second array in descending order and then find the element by iterating. You can use sort() for sorting descending order and find() for getting the element.

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

function customArrayFunction(A, B) {
  // create an additional array variable for storing sorted array, otherwise it will update the order of array B
  var sortB = [];
  B.forEach(function(v) {
    sortB.push(v);
  });

  // sorting array on descending order
  // instead of custom compare function 
  // you can use `sortB.sort().reverse()` simply
  var res = sortB.sort(function(a, b) {
    if (a > b)
      return -1;
    else if (a < b)
      return 1;
    return 0;
  })
  // finding element
  .find(function(v) {
    return A.indexOf(v) > -1;
  });
  
  return res;
}
console.log(customArrayFunction(A,B));
console.log(customArrayFunction(["A","D","G"],["B","D","G"]));


Using ES6 arrow function

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

function customArrayFunction(A, B) {
  var sortB = [];
  B.forEach(v => sortB.push(v));
  var res = sortB.sort((a, b) => {
    if (a > b)
      return -1;
    else if (a < b)
      return 1;
    return 0;
  }).find(v => A.indexOf(v) > -1);

  return res;
}
console.log(customArrayFunction(A, B));
console.log(customArrayFunction(["A", "D", "G"], ["B", "D", "G"]));


More easiest way without using any custom compare function with help of reverse(), which helps to reverse the array.

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

function customArrayFunction(A, B) {
  var sortB = [];
  B.forEach(v => sortB.push(v));
  var res = sortB.sort()
    .reverse()
    .find(v => A.indexOf(v) > -1);

  return res;
}
console.log(customArrayFunction(A, B));
console.log(customArrayFunction(["A", "D", "G"], ["B", "D", "G"]));


UPDATE : If you want to search based on the index then there is no need of sort , just use find()

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

function customArrayFunction(A, B) {
  var res = A
    .find(v => B.indexOf(v) > -1);
  return res;
}
console.log(customArrayFunction(A, B));
console.log(customArrayFunction(["A", "D", "G"], ["B", "D", "G"]));

这篇关于如何从另一个数组的值的数组JavaScript编程在最高的索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆