gpt4 book ai didi

javascript - 在 JS 中对数组进行排序和过滤

转载 作者:行者123 更新时间:2023-11-28 14:32:20 25 4
gpt4 key购买 nike

我有以下代码:

var names = ["John Chuck", "Micheal Novak", "john Owen", "Rick John"];
names = _.sortBy( names, function( name ){
return name;
}

它给了我一个排序的名字列表。现在如果我想进行归档,可以吗?

就像过滤“John”一样,最终列表中只有 3 个带有“John”的名字。

最佳答案

使用 Array.prototype.filter() 尝试以下操作和 Array.prototype.includes() :

var names = ["John Chuck", "Micheal Novak", "john Owen", "Rick John"];
names = _.sortBy( names, function( name ){
return name;
}).filter(n => n.toLowerCase().includes('john'));

console.log(names)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.core.min.js"></script>

关于javascript - 在 JS 中对数组进行排序和过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com