0

I am using a JavaScript library that has some code that expects a simple flat array of items like this...

var items = [
    'item 1',
    'item 2',
    'item 3',
];

This is too limited for my project needs so I am editing the library to accept an array of object instead like this...

var items = [
    {
        id: 1,
        name: 'Item 1',
        image: 'img 1',
    },
    {
        id: 2,
        name: 'Item 2',
        image: 'img 2',
    },
    {
        id: 3,
        name: 'Item 3',
        image: 'img 3',
    },
]; 

The library calls some code like this...

if (_helpers.inArray(val, this.options.items) || (val === '')) {
    return val;
}

It check to see if a string is in the items array.

Because my items array is no longer a simple string value and is instead an object now. This breaks this part of the library code.

The code for the function it calls is this...

var _helpers = {

    inArray: function(val, arr) {
        return ($.inArray(val, arr) !== -1);
    },

};

So I need some help in making my own _helpers.inArray(val, this.options.items) function which will check for a string value in an array of objects under the object property name

Any help appreciated

JasonDavis
  • 48,204
  • 100
  • 318
  • 537

2 Answers2

2

Could be something like this?

var _helpers = {
    inArray: function(val, arr) {
        for (var i = 0; i < arr.length; i++){
            if (arr[i].name === val)
                return true;
        }
        return false;
    },
};

https://jsfiddle.net/z54qnukd/1/

Stefano Castriotta
  • 2,823
  • 3
  • 16
  • 26
1

Try mapping your array of objects back to the simple format:

var _helpers = {

    inArray: function(val, arr) {
        var mappedArr = $.map( items, function(item, index) {
            return item.name;
        });
        return ($.inArray(val, mappedArr) !== -1);
    },

};

https://jsbin.com/tihuyuquni/edit?html,output

pherris
  • 17,195
  • 8
  • 42
  • 58
  • jquery is really overkill for it. `Array.prototype.map` is there for years (and polifyll is available). PS: you could at least use consistent reference to a jquery object - either `jQuery` or `$` in both cases. – zerkms Oct 28 '15 at 22:09
  • @zerkms good catch on the `jQuery` vs `$` - if the OP is already using the library, using it here doesn't seem out of step. – pherris Oct 28 '15 at 22:12
  • In the world where ES2015 is an established standard it's a step back for sure. Compare your code with `items.map(i => i.name).includes(val)` – zerkms Oct 28 '15 at 22:13
  • It *is* awesome :-) The future is here for half a year already – zerkms Oct 28 '15 at 22:15
  • 1
    Hm, looks like MDN has `includes` listed as 'experimental' and as part of ES2016: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes but your point is valid and I need to adjust my thinking a bit. – pherris Oct 28 '15 at 22:21
  • Oh right, thanks for checking that, I wasn't even aware it's not in ES2015 yet :-S – zerkms Oct 28 '15 at 22:23