简体   繁体   English

javascript - 在对象数组中搜索字符串并返回数组

[英]javascript - search for string in array of objects and return array

I would like to write a function that will search the fields of objects in an array for a specific string, adding this object to a new array if the string is found in any of the object's fields.我想编写一个 function 它将在数组中的对象字段中搜索特定字符串,如果在任何对象的字段中找到该字符串,则将此 object 添加到新数组中。 I've gotten my function to work, but was having the issue of the new list containing multiple copies of the objects which contain multiple copies of the string being searched for.我已经让我的 function 工作,但是遇到了包含多个对象副本的新列表的问题,其中包含正在搜索的字符串的多个副本。 I know this is because I've looped it so that each field it finds the string in, it will add the object once more to the new array.我知道这是因为我对它进行了循环,以便它在其中找到字符串的每个字段,它将 object 再次添加到新数组中。 However, I wasn't sure of what alternative way I could go about writing the function to add the object only once, regardless of how many of its fields have matched with the string being searched for.但是,我不确定我可以用什么替代方法 go 关于编写 function 以添加 object 仅一次,无论其字段有多少匹配。 This is my function:这是我的 function:

function search (keyword, array){
    var newArray = [];
    for(let i = 0; i < array.length; i++) {
        for (key in array[i]) {
            if(array[i][key].includes(keyword)){
                newArray.push(array[i]);
            }
        }
    }
    return newArray;
}

An example of where the output is problematic is if I do: output 有问题的一个例子是,如果我这样做:

console.log(search('yes', myArray))

And if myArray contains an object in which 'yes' appears in 3 different fields, it will add this object to newArray 3 times.如果 myArray 包含一个 object,其中“是”出现在 3 个不同的字段中,它会将这个 object 添加到 newArray 3 次。

You could use a Set , this will prevent duplicates.您可以使用Set ,这将防止重复。

function search (keyword, array){
    var result = new Set();
    for(let i = 0; i < array.length; i++) {
        for (key in array[i]) {
            if(array[i][key].includes(keyword)){
                result.add(array[i]);
            }
        }
    }
    return Array.from(result);
}

Improved version of Danny Buonocore code. Danny Buonocore 代码的改进版本。

  • No accidental global variables.没有意外的全局变量。
  • Uses forEach to iterate over the array.使用forEach遍历数组。
  • Uses for...of and Object.values() to iterate over the values of the object (using for..in iterates over all non-Symbol, enumerable properties of an object itself and those the object inherits from its constructor's prototype and are cause for many bugs) Uses for...of and Object.values() to iterate over the values of the object (using for..in iterates over all non-Symbol, enumerable properties of an object itself and those the object inherits from its constructor's prototype and are导致许多错误)
  • "short circuit" the test for adding an object: if a value has matched, there is no need to check the other values. “短路”添加 object 的测试:如果一个值匹配,则无需检查其他值。 This alone would probably solved your problem, but using a set will prevent duplicates if you have the same object multiple times in your array.仅此一项可能就可以解决您的问题,但是如果您的阵列中有多次相同的 object ,则使用set可以防止重复。

 function search (keyword, array){ var result = new Set(); array.forEach( object => { for (const value of Object.values(object)) { if ( value.includes(keyword) ) { result.add(object); continue; // Don't need to check more on this item. } } }); return Array.from(result); } console.log(search("yes", [ { key1:"yes", key2:"yes" }, { key1:"no", key2:"no" } ]));

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

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