Home > database >  How do I search array within an object for any instances of specified string values?
How do I search array within an object for any instances of specified string values?

Time:09-27

How do I search an array for any instances of multiple specified string values?

const arrayOfObjects = [{ 
name: box1,
storage: ['car', 'goat', 'tea']
},
{
name: box2,
storage: ['camel', 'fox', 'tea']
}
];

arrayOfSearchItems = ['goat', 'car', 'oranges'];

If any one or all of the arrayOfSearchItems is present in one of the objects in my array, I want it to either return false or some other way that I can use to excluded that object that is in my arrayOfObjects from a new, filtered arrayOfObjects without any objects that contained the arrayOfSearchItems string values. In this case I would want an array of objects without box1.

Here is what I have tried to do, based on other suggestions. I spent a long time on this. The problem with this function is that it only works on the first arrayOfSearchItems strings, to exclude that object. It will ignore the second or third strings, and not exclude the object, even if it contains those strings. For example, it will exclude an object with 'goat'. Once that happens though, it will no longer exclude based on 'car'. I have tried to adapt my longer code for the purposes of this question, I may have some typos.

const excludeItems = (arrayOfSearchItems, arrayOfObjects) => {
    let incrementArray = [];
    let userEffects = arrayOfSearchItems;
    let objects = arrayOfObjects;
    
    for (i = 0; i < userEffects.length; i  ) {
            for (x = 0; x < objects.length; x  ) {
                if (objects[x].storage.indexOf(userEffects) <= -1) {
                
                    incrementArray.push(objects[x]); 
                }
        }

    }
    
    return(incrementArray);
}

let filteredArray = excludeItems(arrayOfSearchItems, arrayOfObjects);
console.log(filteredArray); 

CodePudding user response:

Thanks for providing some example code. That helps.

Let's start with your function, which has a good signature:

const excludeItems = (arrayOfSearchItems, arrayOfObjects) => { ... }

If we describe what this function should do, we would say "it returns a new array of objects which do not contain any of the search items." This gives us a clue about how we should write our code.

Since we will be returning a filtered array of objects, we can start by using the filter method:

return arrayOfObjects.filter(obj => ...)

For each object, we want to make sure that its storage does not contain any of the search items. Another way to word this is "every item in the starage array does NOT appear in the list of search items". Now let's write that code using the every method:

.filter(obj => {
  // ensure "every" storage item matches a condition
  return obj.storage.every(storageItem => {
    // the "condition" is that it is NOT in the array search items
    return arrayOfSearchItems.includes(storageItem) === false);
  });
});

Putting it all together:

const excludeItems = (arrayOfSearchItems, arrayOfObjects) => {
    return arrayOfObjects.filter(obj => {
        return obj.storage.every(storageItem => {
            return arrayOfSearchItems.includes(storageItem) === false;
        });
    });
}

Here's a fiddle: https://jsfiddle.net/3p95xzwe/

CodePudding user response:

You can achieve your goal by using some of the built-in Array prototype functions, like filter, some and includes.

const excludeItems = (search, objs) => 
  objs.filter(({storage:o}) => !search.some(s => o.includes(s)));

In other words: Filter my array objs, on the property storage to keep only those that they dont include any of the strings in search.

  • Related