使用Underscore获取满足特定条件的元素的所有索引
Getting all indexes of elements fulfilling a certain condition using Underscore
我想检索满足特定条件的数组中元素的所有索引。例如,如果我有一个像这样的数组:
var countries = ["India", "Australia", "United States", "Brazil"];
而且我想获取那些字符串长度大于 5 的元素的索引,我应该取回一个像
这样的数组
[2, 3]
甚至像这样的对象:
{
2: "United States",
3: "Australia"
}
JavaScript 或 underscore.js 中是否有我可以利用的 inbuilt/native 函数?
试试这个(纯 JavaScript):
var countries = ["India", "Australia", "United States", "Brazil"];
var result = [];
for(var i = 0; i < countries.length; i++)
{
if(countries[i].length > 5) {
result.push(i);
console.log(countries[i]);
}
}
console.log(result);
或与jQuery相同:
var sum=[];
$.each( ["India", "Australia", "United States", "Brazil"], function( index, value ){
value.length > 5 && sum.push(value);
});
console.log(sum);
在 vanilla JS 中,获取索引:
var longnames = ["India", "Australia", "United States", "Brazil"]
.map( function( item, idx ){
// convert the names to indexes if name is linger than 5 characters
return ( item.length > 5) ? idx : null
}).filter(function(item){
// fiter out the nulls
return item;
});
// returns [1,2,3] since "Australia".length > 5
由于下划线同时具有 .map
和 .reduce
方法,因此您应该能够 "underscorize" 此解决方案 ;)
以 { index : name }
:
的形式创建对象
var longnames = ["India", "Australia", "United States", "Brazil"]
.reduce( function( ret, name, idx ){
if( name.length > 5 ){
ret[ idx ] = name;
}
return ret;
}, {});
/* returns
Object { 1: "Australia", 2: "United States", 3: "Brazil" }
*/
替代 .reduce 使用下划线:
var longnames = _.reduce(["India", "Australia", "United States", "Brazil"],
function( ret, name, idx ){
if( name.length > 5 ){
ret[ idx ] = name;
}
return ret;
}, {});
我想检索满足特定条件的数组中元素的所有索引。例如,如果我有一个像这样的数组:
var countries = ["India", "Australia", "United States", "Brazil"];
而且我想获取那些字符串长度大于 5 的元素的索引,我应该取回一个像
这样的数组[2, 3]
甚至像这样的对象:
{
2: "United States",
3: "Australia"
}
JavaScript 或 underscore.js 中是否有我可以利用的 inbuilt/native 函数?
试试这个(纯 JavaScript):
var countries = ["India", "Australia", "United States", "Brazil"];
var result = [];
for(var i = 0; i < countries.length; i++)
{
if(countries[i].length > 5) {
result.push(i);
console.log(countries[i]);
}
}
console.log(result);
或与jQuery相同:
var sum=[];
$.each( ["India", "Australia", "United States", "Brazil"], function( index, value ){
value.length > 5 && sum.push(value);
});
console.log(sum);
在 vanilla JS 中,获取索引:
var longnames = ["India", "Australia", "United States", "Brazil"]
.map( function( item, idx ){
// convert the names to indexes if name is linger than 5 characters
return ( item.length > 5) ? idx : null
}).filter(function(item){
// fiter out the nulls
return item;
});
// returns [1,2,3] since "Australia".length > 5
由于下划线同时具有 .map
和 .reduce
方法,因此您应该能够 "underscorize" 此解决方案 ;)
以 { index : name }
:
var longnames = ["India", "Australia", "United States", "Brazil"]
.reduce( function( ret, name, idx ){
if( name.length > 5 ){
ret[ idx ] = name;
}
return ret;
}, {});
/* returns
Object { 1: "Australia", 2: "United States", 3: "Brazil" }
*/
替代 .reduce 使用下划线:
var longnames = _.reduce(["India", "Australia", "United States", "Brazil"],
function( ret, name, idx ){
if( name.length > 5 ){
ret[ idx ] = name;
}
return ret;
}, {});