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

4
  • Try to use this:Javascript search inside a JSON object, it should help you Commented Oct 28, 2015 at 21:42
  • Where does 'options' come into this? this.options.items Commented Oct 28, 2015 at 21:45
  • @AdamJeffers I think var items is actually the contents of this.options.items. Commented Oct 28, 2015 at 21:48
  • items.map(i => i.name).includes(val) :-) Commented Oct 28, 2015 at 22:13

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

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 good catch on the jQuery vs $ - if the OP is already using the library, using it here doesn't seem out of step.
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)
It is awesome :-) The future is here for half a year already
Hm, looks like MDN has includes listed as 'experimental' and as part of ES2016: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… but your point is valid and I need to adjust my thinking a bit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.