1.0.1 • Published 6 years ago

exists-in-array v1.0.1

Weekly downloads
1
License
Unlicense
Repository
github
Last release
6 years ago

exists-in-array

Check if a value exists in an array

Usage

npm install exists-in-array --save

require('exists-in-array')( < Source array > , < Attribute to check inside objects in array or false > , < value to search for > , < use explicit mode (bool) >

const exists-in-array = require('exists-in-array');
var withObject = [
    {
        a: "foo",
        b: "bar"
    },
    {
        a: "bar",
        b: "bar bar"
    },
    {
        a: 5,
        b: 17
    },
    {
        a: '7',
        b: '18'
    }
];
var withoutObject = [
    'a',
    'b',
    'foo',
    'bar',
    5,
    178,
    '14'
];
existsInArray(withObject, 'a', 'bar') // Returns true
existsInArray(withObject, 'b', 'foo') // Returns false
existsInArray(withoutObject, undefined, 'bar') // Returns true
existsInArray(withoutObject, undefined, 65) // Returns false
existsInArray(withoutObject, undefined, '5') // Returns true
existsInArray(withoutObject, undefined, 5) // Returns true
// EXPLICIT MODE
existsInArray(withObject, 'a', 'bar', true) // Returns true
existsInArray(withObject, 'b', 'foo', true) // Returns false
existsInArray(withObject, 'a', '5', true) // Returns false
existsInArray(withoutObject, undefined, 5, true) // Returns true
existsInArray(withoutObject, undefined, '5', true) // Returns false