如何使用 javascript 编程从另一个数组的值中获取数组中最高索引的值

How to get value at highest index in a array from the values of another array with javascript programming

本文关键字:数组 高索引 索引 获取 编程 另一个 javascript 何使用      更新时间:2023-09-26

我有 2 组数组

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

现在我必须从var B中得到一个值,该值在var A中处于最高索引,它是J

我必须通过JavaScript编程找出var B J

提前致谢

按降序对第二个数组进行排序,然后通过迭代找到该元素。您可以使用sort()对降序进行排序,并使用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) {
  // 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"]));


使用 ES6 箭头功能

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"]));


reverse() 的帮助下不使用任何自定义比较函数的更简单方法,这有助于反转数组。

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"]));


更新:如果要根据索引进行搜索,则无需排序,只需使用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"]));

用于每个列表以获得您想要的结果。

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
var B = ["B", "J", "A", "C", "G"];
var index = -1; // find index of your array A
A.forEach(function(item1, index1) {
    B.forEach(function(item2, index2) {
        if (item1 === item2) {
            index = index1 > index ? index1 : index;
        }
    })
})
var result = index >= 0 ? A[index] : 'No Result found'; // gives your desired result.

技术含量可能有点太低了,但是以下内容呢(console.logging it for testing)

    var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
    var B = ["B", "J", "A", "C", "G"];
    var max=0;
    var letter="";
    for(i=0;i<B.length;i++)
       {
          var test=A.indexOf(B[i]);
          if(test > max){max=test;letter=B[i]};
       }
    console.log(letter)

控制台.log按预期给出"J"

这是一个带有临时对象和一些数组方法的解决方案。

var a = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
    b = ["B", "J", "A", "C", "G"],
    result = function (array1, array2) {
        var o = {},
            r;
        array2.forEach(function (a) {
            o[a] = true;
        });
        a.reverse().some(function (a) {
            if (o[a]) {
                r = a;
                return true;
            }
        });
        return r;
    }(a, b);
document.write(result);