如何使用 javascript 编程从另一个数组的值中获取数组中最高索引的值
How to get value at highest index in a array from the values of another array with javascript programming
我有 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);
相关文章:
- Javascript中的多维数组排序索引问题
- 使用JQuery检查数组的索引中是否存在字符串
- 我想将数组的索引附加到
中,其中选择选项是数组的索引 - 获取字符串中的匹配项数组作为索引
- 重新排列数组的索引以匹配其他数组
- symfony 2 trick访问数组javascript索引
- 推送数组中索引处的值
- 在Javascript中重新建立对象数组的索引(无拼接)
- JavaScript数组在索引处拆分
- 在Javascript中获取关联数组数组的索引
- 只需要重置Javascript数组的索引
- <选择>其中选择等于源数组的索引
- 在循环中调用数组的索引导致错误
- 离子弹出窗口中数组的索引更改
- Javascript - 将多维数组与索引相结合
- Javascript - 数组中索引周围的范围为 5
- 在我的情况下,如何检测数组的索引
- 如何在 JavaScript 中从对象数组及其索引中获取最低值
- 快速从 2D 数组获取索引,无需迭代 jquery
- 如果重复,只返回数组中索引高的元素