javascript - Javascript的“like”函数针对数组

标签 javascript

我已经阅读了堆栈溢出中的其他项目,到目前为止,没有任何工作,或者可能我听不懂。

我希望用户能够在字段中键入一个值,这样可以检查数组中的每个项目,然后返回匹配的项目。

这是数组的示例。

[{
"ID": 249,
"SetNumber": 7993,
"URL": "https://www.lego.com/biassets/bi/4516696.pdf",
"Images": "https://www.lego.com/biassets/biimg/4516696.png",
"Description": "Service Station",
"Notes": "BUILD.INSTR.3006,  7993 NA 2/2",
"DashedNumber": "7993-1"
 },
 {
"ID": 250,
"SetNumber": 7994,
"URL": "https://www.lego.com/biassets/bi/4516447.pdf",
"Images": "https://www.lego.com/biassets/biimg/4516447.png",
"Description": "LEGO City Harbor",
"Notes": "BUILD.INSTR.3006, 7994 2/3",
"DashedNumber": "7994-1"
 },
 {
"ID": 251,
"SetNumber": 7994,
"URL": "https://www.lego.com/biassets/bi/4516451.pdf",
"Images": "https://www.lego.com/biassets/biimg/4516451.png",
"Description": "LEGO City Harbor",
"Notes": "BUILD.INSTR.3006, 7994 3/3",
"DashedNumber": "7994-1"
 }]


这样客户可以输入Lego,然后显示后两个。

到目前为止,这是我的代码

$(document).ready(function () {
$('#search-now').click(function () {
    var newArray = obj.results.filter(function (el) {
        return el.ID &&
            el.SetNumber &&
            el.Images &&
            el.URL &&
            el.Description == $('#dynamId').val() &&
            el.Notes &&
            el.DashedNumber;

    });


但这只会返回完全匹配,而不是“喜欢”匹配。

最佳答案

您应该使用indexOftoLowerCase(如果不区分大小写),而不只是==

示例(使用Lodash filter方法;可以与您的find方法互换)



let legoProducts = [{
    "ID": 249,
    "SetNumber": 7993,
    "URL": "https://www.lego.com/biassets/bi/4516696.pdf",
    "Images": "https://www.lego.com/biassets/biimg/4516696.png",
    "Description": "Service Station",
    "Notes": "BUILD.INSTR.3006,  7993 NA 2/2",
    "DashedNumber": "7993-1"
 },
 {
    "ID": 250,
    "SetNumber": 7994,
    "URL": "https://www.lego.com/biassets/bi/4516447.pdf",
    "Images": "https://www.lego.com/biassets/biimg/4516447.png",
    "Description": "LEGO City Harbor",
    "Notes": "BUILD.INSTR.3006, 7994 2/3",
    "DashedNumber": "7994-1"
 },
 {
    "ID": 251,
    "SetNumber": 7994,
    "URL": "https://www.lego.com/biassets/bi/4516451.pdf",
    "Images": "https://www.lego.com/biassets/biimg/4516451.png",
    "Description": "LEGO City Harbor",
    "Notes": "BUILD.INSTR.3006, 7994 3/3",
    "DashedNumber": "7994-1"
 }];

let searchString = 'Lego';
let result = _.filter(legoProducts, function(object) {
 return object.Description.toLowerCase().indexOf(searchString.toLowerCase()) != -1;
});
console.log(result);

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

关于javascript - Javascript的“like”函数针对数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55070817/

相关文章:

javascript - 遍历Jquery中启用的选项卡

javascript - 点击浏览器 "back"按钮显示 JSON 而不是 HTML(使用 Rails 和 d3.js)

javascript - safari 显示 javascript 源代码而不是执行

javascript - 没有在状态中设置属性?

javascript - Javascript单选/复选框问题

javascript - 语言变更负担

javascript - 如何使用点击事件在 table 上获得值(value)单选?

javascript onsubmit 不起作用

javascript - Javascript-将所有HTML代码复制到剪贴板

javascript - 根据其他元素状态/类有条件地向元素添加类