我想找到一种比使用2次迭代在对象中查找值更好,更优雅的解决方案。我查看了要在数组中搜索的函数,例如find,indexOf,但想知道我可以不使用for或forEach来搜索对象。
说我想找到所有游泳者。我到目前为止所得到的。
members = [
{ name: "Sue", hobby: ["Swimming", "Running", "Dancing"] },
{ name: "Sam", hobby: ["Cars", "Travelling"] },
{ name: "John", hobby: ["Reading", "Swimming"] },
{ name: "Rob", hobby: ["Running", "Coding"] },
];
function findSwimmers(members, hobby) {
let swimmers = [];
members.forEach(function (e){
e.hobby.forEach(function (element){
if (element.toLowerCase() === hobby.toLowerCase()) {
swimmers.push(e);
}
});
});
return swimmers;
}
答案 0 :(得分:5)
您可以通过传递回调函数作为参数来将filter
与some
结合使用。
let members = [
{ name: "Sue", hobby: ["Swimming", "Running", "Dancing"] },
{ name: "Sam", hobby: ["Cars", "Travelling"] },
{ name: "John", hobby: ["Reading", "Swimming"] },
{ name: "Rob", hobby: ["Running", "Coding"] },
];
function findSwimmers(members, hobby) {
return members_swimming = members.filter(({hobby}) => hobby.some(item => item == "Swimming"));
}
console.log(findSwimmers(members, "Swimming"));
答案 1 :(得分:2)
您可以将let swimmers = members.filter(person => person.hobby.includes('Swimming'));
和Array.filter()
用于解构:
Array.map()
答案 2 :(得分:1)
任何解决方案都会涉及到循环。唯一的问题是循环是在您的代码中还是在您调用的函数中。
function findSwimmers(members, hobby) {
hobby = hobby.toLowerCase();
return members.filter(member =>
member.hobby.some(h => h.toLowerCase() === hobby)
);
}
您可能需要其他函数名称。 :-)
实时示例:
const members = [
{ name: "Sue", hobby: ["Swimming", "Running", "Dancing"] },
{ name: "Sam", hobby: ["Cars", "Travelling"] },
{ name: "John", hobby: ["Reading", "Swimming"] },
{ name: "Rob", hobby: ["Running", "Coding"] },
];
function findSwimmers(members, hobby) {
hobby = hobby.toLowerCase();
return members.filter(member =>
member.hobby.some(h => h.toLowerCase() === hobby)
);
}
console.log(findSwimmers(members, "swimming"));
.as-console-wrapper {
max-height: 100% !important;
}
请注意,您的原始代码在发现一个爱好匹配时并没有停止循环(并且如果爱好数组中存在重复的条目,则该同一个成员将被添加一次以上。)但是,这并不意味着在该数组中重复)。
如果可能,您也可以将String#localeCompare
与sensitivity: "base"
选项一起使用,而不是比较小写的字符串。
答案 3 :(得分:0)
您只需一行即可实现:
id += letters + (++numbers).ToString("D4");
答案 4 :(得分:0)
第一个解决方案:有点破解但嘿,没有循环
第二个解决方案:使用Array.findIndex
可能会减少内部循环中的迭代次数(嗯,至少会为您提供更少迭代次数的概率),因为(MDN :
...如果找到了这样的元素,则findIndex 立即返回索引 对于那个迭代...
const findMembersWithValueForKey = (members, key, value) =>
members.filter(member => member[key] instanceof Array
&& `#${member[key].join("#")}#`.toLowerCase()
.indexOf(`#${value.toLowerCase()}#`) > -1
|| false);
const findMembersWithValueForKey2 = (members, key, value) => {
value = value.toLowerCase();
return members.filter(member =>
member[key] instanceof Array
&& member[key].findIndex(v => v.toLowerCase() === value) > -1
|| false);
};
const members = [
{ name: "Sue", hobby: ["Swimming", "Running", "Dancing"] },
{ name: "Sam", hobby: ["Cars", "Travelling"] },
{ name: "John", hobby: ["Reading", "Swimming"] },
{ name: "Rob", hobby: ["Running", "Coding"] },
];
console.log(findMembersWithValueForKey(members, "hobby", "swimming"));
console.log(findMembersWithValueForKey2(members, "hobby", "swimming"));