0

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); 
TheKleyn
  • 29
  • 4
  • 1
    If you don't want this closed, your best chances are to 1) format your code and 2) provide some example code that shows you at least tried. What you are doing is asking someone to write some array filtering logic for you, which is something that has definitely been answered before. The problem I see here is that you haven't made any attempt to do this yourself or understand how other answers on the web can be applied to your situation. – Ryan Wheale Sep 26 '22 at 18:08
  • thanks, I have updated with what I have been working on for a long time. I have tried, pretty hard. – TheKleyn Sep 26 '22 at 18:46
  • Please show one example of what is the desired output of calling your code, and what is the wrong output you are currently getting. It is not clear from the text. – Rodrigo Rodrigues Sep 26 '22 at 19:21

2 Answers2

1

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/

Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96
0

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.

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36