基于索引数组过滤数组
Filter array based on an array of index
首先,如果它是重复的,我深表歉意(我搜索过但没有找到这个简单的例子......),但我想根据 [=] 中的索引 select arr1
的元素15=]:
arr1 = [33,66,77,8,99]
arr2 = [2,0,3]
我正在使用 underscore.js
但未检索到 0
索引(似乎被视为 false
):
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return index;
}
});
哪个returns:
# [77, 8]
我该如何解决这个问题,是否有更简单的方法来使用索引数组进行过滤?我期待以下结果:
# [77, 33, 8]
您正在 returning index
,因此在您的情况下 0
被视为 false
。所以你需要 return true
而不是
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return true;
}
});
或 return _.contains()
res = _.filter(arr1, function(value, index){
return _.contains(arr2, index);
});
_.contains
return 是一个布尔值。您应该 return 来自 filter
谓词,而不是索引,因为 0
是 falsy value.
res = _.filter(arr1, function(value, index)) {
return _.contains(arr2, index);
});
顺便说一句,JavaScript 数组有一个原生的 filter
方法,所以你可以使用:
res = arr1.filter(function(value, index)) {
return _.contains(arr2, index);
});
作为主循环遍历索引数组不是更好吗?
var arr1 = [33,66,77,8,99]
var arr2 = [2,0,3]
var result = [];
for(var i=0; i<arr2.length; i++) {
var index = arr2[i];
result.push(arr1[index]);
}
console.log(result);
最简单的方法是在arr2
上使用_.map
,像这样
console.log(_.map(arr2, function (item) {
return arr1[item];
}));
// [ 77, 33, 8 ]
在这里,我们迭代索引并从 arr1
中获取相应的值并创建一个新数组。
等同于上面的,但也许更高级一点,就是使用_.propertyOf
代替匿名函数:
console.log(_.map(arr2, _.propertyOf(arr1)));
// [ 77, 33, 8 ]
如果您的环境支持 ECMA Script 6 的 Arrow 函数,那么您也可以这样做
console.log(_.map(arr2, (item) => arr1[item]));
// [ 77, 33, 8 ]
此外,如果您的目标环境支持它们,您可以使用本机 Array.protoype.map
本身,就像这样
console.log(arr2.map((item) => arr1[item]));
// [ 77, 33, 8 ]
对我来说,最好的方法是使用 filter。
let z=[10,11,12,13,14,15,16,17,18,19]
let x=[0,3,7]
z.filter((el,i)=>x.some(j => i === j))
//result
[10, 13, 17]
可以在想要子集化的数组上使用 filter
方法。 filter
遍历数组和 returns 一个由通过测试的项目组成的新数组。测试是一个回调函数,在下面的示例中是一个匿名箭头函数,它接受必需的 currentValue
和可选的 index
参数。在下面的示例中,我使用 _
作为第一个参数,因为它未被使用,这样 linter 就不会将其突出显示为未使用 :).
在回调函数中,数组的 includes
方法用于我们用作索引源的数组,以检查 arr1
的当前索引是否是所需索引的一部分。
let arr1 = [33, 66, 77, 8, 99];
let arr2 = [2, 0, 3];
let output = arr1.filter((_, index) => arr2.includes(index));
console.log("output", output);
首先,如果它是重复的,我深表歉意(我搜索过但没有找到这个简单的例子......),但我想根据 [=] 中的索引 select arr1
的元素15=]:
arr1 = [33,66,77,8,99]
arr2 = [2,0,3]
我正在使用 underscore.js
但未检索到 0
索引(似乎被视为 false
):
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return index;
}
});
哪个returns:
# [77, 8]
我该如何解决这个问题,是否有更简单的方法来使用索引数组进行过滤?我期待以下结果:
# [77, 33, 8]
您正在 returning index
,因此在您的情况下 0
被视为 false
。所以你需要 return true
而不是
res = _.filter(arr1, function(value, index){
if(_.contains(arr2, index)){
return true;
}
});
或 return _.contains()
res = _.filter(arr1, function(value, index){
return _.contains(arr2, index);
});
_.contains
return 是一个布尔值。您应该 return 来自 filter
谓词,而不是索引,因为 0
是 falsy value.
res = _.filter(arr1, function(value, index)) {
return _.contains(arr2, index);
});
顺便说一句,JavaScript 数组有一个原生的 filter
方法,所以你可以使用:
res = arr1.filter(function(value, index)) {
return _.contains(arr2, index);
});
作为主循环遍历索引数组不是更好吗?
var arr1 = [33,66,77,8,99]
var arr2 = [2,0,3]
var result = [];
for(var i=0; i<arr2.length; i++) {
var index = arr2[i];
result.push(arr1[index]);
}
console.log(result);
最简单的方法是在arr2
上使用_.map
,像这样
console.log(_.map(arr2, function (item) {
return arr1[item];
}));
// [ 77, 33, 8 ]
在这里,我们迭代索引并从 arr1
中获取相应的值并创建一个新数组。
等同于上面的,但也许更高级一点,就是使用_.propertyOf
代替匿名函数:
console.log(_.map(arr2, _.propertyOf(arr1)));
// [ 77, 33, 8 ]
如果您的环境支持 ECMA Script 6 的 Arrow 函数,那么您也可以这样做
console.log(_.map(arr2, (item) => arr1[item]));
// [ 77, 33, 8 ]
此外,如果您的目标环境支持它们,您可以使用本机 Array.protoype.map
本身,就像这样
console.log(arr2.map((item) => arr1[item]));
// [ 77, 33, 8 ]
对我来说,最好的方法是使用 filter。
let z=[10,11,12,13,14,15,16,17,18,19]
let x=[0,3,7]
z.filter((el,i)=>x.some(j => i === j))
//result
[10, 13, 17]
可以在想要子集化的数组上使用 filter
方法。 filter
遍历数组和 returns 一个由通过测试的项目组成的新数组。测试是一个回调函数,在下面的示例中是一个匿名箭头函数,它接受必需的 currentValue
和可选的 index
参数。在下面的示例中,我使用 _
作为第一个参数,因为它未被使用,这样 linter 就不会将其突出显示为未使用 :).
在回调函数中,数组的 includes
方法用于我们用作索引源的数组,以检查 arr1
的当前索引是否是所需索引的一部分。
let arr1 = [33, 66, 77, 8, 99];
let arr2 = [2, 0, 3];
let output = arr1.filter((_, index) => arr2.includes(index));
console.log("output", output);