0

I am working with some data for Items, and unfortunately each Item has a property called medium, and the value for medium can be one of instore, instore,online, or online.

I am trying to create a filter, but I am not sure the best way to iterate through all the list of Items I get back (100 at a time) to check if they instore, instore,online, or online.

If I select instore from the drop-down, how can I iterate through my array of 100 Items to find which ones have medium: "instore" and also medium: "instore,online"?

For example, if I select "instore" then do I check each item for "instore" OR "instore,online"? And if I choose online, check "online" or "instore,online"? Doesn't this seem really bad way to do things?

How do I handle the comma separated?

user1354934
  • 8,139
  • 15
  • 50
  • 80

4 Answers4

1

If you can use third party utility library, I would use lodash filter function:

var items = [
  { 'data': 'data1', 'medium': 'instore' },
  { 'data': 'data2', 'medium': 'online' }
];


var filtered = _.filter(items, { 'medium': 'instore' });
Edmar Miyake
  • 12,047
  • 3
  • 37
  • 38
1

In your particular case, I would make filter results for instore use the following logic.

if (medium != "online")

This way you avoid checking the two other possible conditions.

If there was the possibility for additional values, I would parse each and create a set and use .has('value').

Philip Tinney
  • 1,986
  • 17
  • 19
0

With plain ordinary JavaScript you can use the filter method on the array. For example:

    function instoreNotMore() {
        var ary = ['instore','instore,online','instore','online', 'online'];
        var newAry = ary.filter(function(s){
            if(s.includes('instore')) {
                return s;
            }
        });
        console.log(newAry);
    }

This method filters the array ary which contains all the values you mentioned for strings containing the substring instore which will cover both instore and instore,online. The commas aren't a factor. The strings containing instore will be added to a new array called newAry.

Phil
  • 3,568
  • 3
  • 34
  • 40
  • Actually using `if (medium != "online")` as Philip (awesome name btw) mentioned below will be quicker. – Phil Oct 08 '16 at 22:14
0

There are many ways to do this, in this case you could use the filter method combined with indexOf to check for the occurrence of the selected string. For more details about using indexOf to find substrings check How to check whether a string contains a substring in JavaScript?

var items = [
  { 'data': 'data1', 'medium': 'instore' },
  { 'data': 'data1', 'medium': 'instore,online' },
  { 'data': 'data2', 'medium': 'online' }
];

var filtered = items.filter(function (obj) {
  return obj.medium.indexOf('instore') !== -1; 
});

As soon as the property contains 'instore' it will pass through the filter, which works in your example unless there are more cases to check for...

To choose the best way it also depends on what platform you are developing for (browser support) and what tools your are using (eg. if you write ES2015 code and use Babel to transpile it).

Community
  • 1
  • 1
Robin Venneman
  • 383
  • 1
  • 7