简体   繁体   English

javascript在数组中查找具有特定属性的对象

[英]javascript find an object with specific properties in an array

I need to make an extension to existing code, can't change it. 我需要对现有代码进行扩展,无法更改。 There's this array: 有这个数组:

var availableTags = [
        { label: "Yoga classes", category: "EDUCATIONAL" },
        { label: "Cooking classes", category: "EDUCATIONAL" },
        { label: "Cheese tastings", category: "EDUCATIONAL" },
        { label: "Maker Workshops", category: "PRACTICAL" },
        { label: "Seminars", category: "PRACTICAL" },
        //many more of these
];

Now I need to check if a text entered in an input box is included in one of the labels, eg if the user enters "Yoga classes" => OK, if "Yoga" => NOK, "sdsdf" => NOK, etc. 现在,我需要检查是否在标签之一中包含了在输入框中输入的文本,例如,用户是否输入了“瑜伽课” =>确定,如果“瑜伽” =>否,“ sdsdf” =>否,等等。 。

What is the best way to do this? 做这个的最好方式是什么? I am not sure I can use Array.indexOf as I am not sure how to pass the Object to the function, I would try looping through the array (around 40 entries) and compare each object. 我不确定我可以使用Array.indexOf,因为不确定如何将Object传递给函数,我将尝试遍历数组(大约40个条目)并比较每个对象。

You need to loop over every item in availableTags and check whether that item's label is equal to some input. 您需要遍历availableTags每个项目,并检查该项目的label是否等于某些输入。 Try something like this: 尝试这样的事情:

var input = "Yoga classes";
var found = false;
for (var i = 0, j = availableTags.length; i < j; i++) {
    var cur = availableTags[i];
    if (cur.label === input) {
        found = true;
        break;
    }
}
console.log(found);

DEMO: http://jsfiddle.net/k4cp4/4/ 演示: http : //jsfiddle.net/k4cp4/4/

Where this can easily be put into a function, like: 可以很容易地将其放入函数中的地方,例如:

var checkMatch = (function () {
    var availableTags = [
        { label: "Yoga classes", category: "EDUCATIONAL" },
        { label: "Cooking classes", category: "EDUCATIONAL" },
        { label: "Cheese tastings", category: "EDUCATIONAL" },
        { label: "Maker Workshops", category: "PRACTICAL" },
        { label: "Seminars", category: "PRACTICAL" }
    ];

    return function (input) {
        var found = false;
        for (var i = 0, j = availableTags.length; i < j; i++) {
            var cur = availableTags[i];
            if (cur.label === input) {
                found = true;
                break;
            }
        }
        return found;
    };
})();

DEMO: http://jsfiddle.net/k4cp4/5/ 演示: http : //jsfiddle.net/k4cp4/5/

This checks for an exact match. 这会检查是否完全匹配。 So if you want a case insensitive match, you can use: 因此,如果您希望使用不区分大小写的匹配,则可以使用:

if (cur.label.toLowerCase() === input.toLowerCase()) {

DEMO: http://jsfiddle.net/k4cp4/6/ 演示: http : //jsfiddle.net/k4cp4/6/

If you want to see if any of the label s contain the input, you can use indexOf like: 如果要查看任何label s是否包含输入,可以使用indexOf例如:

if (cur.label.indexOf(input) > -1) {

DEMO: http://jsfiddle.net/k4cp4/7/ 演示: http : //jsfiddle.net/k4cp4/7/

You can use Array.some method: 您可以使用Array.some方法:

Tests whether some element in the array passes the test implemented by the provided function. 测试数组中的某些元素是否通过提供的功能实现的测试。

Then your code would look something like: 然后您的代码将类似于:

var isFound = availableTags.some(function(el) {
    return el.label === 'Yoga classes';
});

Note: some method needs to be shimmed. 注意: 某些方法需要填充。

var check = function(item) {    
    for(at in availableTags) {
       if(item == availableTags[at].label) {
          return true;
       }
    }
    return false;
}


console.log(check("Yoga classes"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM