获取两个数组中相似值的索引
getting the index of the similar values of 2 arrays
我想获得array1和array2的相似值的索引,并将其存储到stored_index变量
array1 = ["50","51","52","53","54","55","56","57","58","59"];
array2 = ["59","55","51"];
存储的索引应该是这样的。
stored_index = [9,5,1]; // index 9,5,1 is equal to their indices in array 1
Javascript map()
可以这样做:
var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var stored_index = array2.map(function(val){
return array1.indexOf(val)
}).filter(function(val){
return (val != -1)
});
console.log(stored_index)
移除filter
:
var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var stored_index = array2.map(function(val){
return (array1.indexOf(val) != -1) ? array1.indexOf(val) : null;
});
console.log(stored_index)//output [9, 5, 1]
<<p> 更新演示/strong> 尝试使用Array.prototype.map()
和Array.prototype.indexOf()
来实现你想要的,
var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var stored_index = array2.map(function(val){
return array1.indexOf(val);
});
演示循环遍历数组2并使用IndexOf函数
var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var output = [];
for(var item in array2){
output.push(array1.indexOf(array2[item]));
}
如果您碰巧需要索引列表和交集,您可以将交集存储在a3数组中,然后对其进行索引。
var array1 = ["50", "51", "52", "53", "54", "55", "56", "57", "58", "59"];
var array2 = ["59", "55", "51"];
var a3 = array1.filter(function (n) {
return array2.indexOf(n) !== -1
});
alert(a3);
stored_index = [];
var i = 0;
for (; i < a3.length; i++) {
stored_index.push(array1.indexOf(a3[i]));
}
alert(stored_index);
对于已有的答案,我建议:
var array1 = ["50", "51", "52", "53", "54", "55", "56", "57", "58", "59"],
array2 = ["59", "55", "51"];
// named function, to which arguments are passed:
function getIndicesOfCommonArrayValues(one, two) {
// if the named arguments both match the
// assessment within the Array.prototype.every()
// anonymous function we proceed, otherwise we
// do nothing; if every assessment performed
// returns true, then the method itself returns
// a Boolean true:
if ([one, two].every(function (arr) {
// 'arr' is the current array-value of the array
// over which we're iterating:
// here check that the typeof arr is not 'undefined', AND
// that the constructor of arr is Array; this ensures that
// we have two defined array-values, and that the array
// value we're testing is itself an Array:
return 'undefined' !== typeof arr && arr.constructor === Array;
})) {
// here we find the longest array:
var longestArray = one.length >= two.length ? one : two,
// here we find the shortest array:
shortestArray = one.length < two.length ? one : two;
// we return the array returned by Array.prototype.map(),
// which iterates over the shortest array:
return shortestArray.map(function (value) {
// 'value' is the current array-value of
// the array over which we're iterating.
// if the longestArray contains the current
// array-value:
if (longestArray.indexOf(value) > -1) {
// we return the index of that array
// value to the array we're creating
// Array.prototype.map():
return longestArray.indexOf(value);
}
});
}
}
console.log(getIndicesOfCommonArrayValues(array1, array2));
var array1 = ["50", "51", "52", "53", "54", "55", "56", "57", "58", "59"],
array2 = ["59", "55", "51"];
function getIndicesOfCommonArrayValues(one, two) {
if ([one, two].every(function (arr) {
return 'undefined' !== typeof arr && arr.constructor === Array;
})) {
var longestArray = one.length >= two.length ? one : two,
shortestArray = one.length < two.length ? one : two;
return shortestArray.map(function (value) {
if (longestArray.indexOf(value) > -1) {
return longestArray.indexOf(value);
}
});
}
}
console.log(getIndicesOfCommonArrayValues(array1, array2));
外部JS小提琴演示实验和开发。
引用:
-
Array.prototype.every()
. -
Array.prototype.indexOf()
. -
Array.prototype.map()
. -
Object.prototype.constructor
.
相关文章:
- 名称输入的索引
- 在jQuery中获取表的行索引
- 测试索引值是否等于某个数字的倍数
- 循环遍历数组中的特定索引
- 按照选项卡索引的顺序循环一个jQuery选择
- 在JavaScript中通过索引从对象数组中获取值
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- 在索引.html和应用.js [node.js] 之间共享变量
- 如何为高图中的区域线创建z索引
- 下拉列表在使用z索引放置在前面后停止工作
- 我如何制作一个JS函数,它可以从相似的原始颜色双向更改为某个颜色
- 减去两个索引不同但值相同的整数
- 如何通过所选索引(AngularJS)在模态弹出窗口中显示数据
- Javascript:根据对象的嵌套数组中的值,在数组中查找对象的索引
- 用javascript查询lucene索引
- 使用它更新集合中的嵌套数组's索引
- 如何将ng选项的索引作为angularJs中的值传递给模型
- Javascript获取所有锚链接的索引
- 多维关联数组的最后一个索引
- 获取两个数组中相似值的索引