1

I have the following array:

var data = [
    'Value1',
    'Value2',
    'Value3'
];

Using another array, how would I get a truthy value if a value was found within the data array?

var dataLookup = [
    'Value1',
    'Value2'
]

I know that in lodash I could do the following to look for a single value;

_.includes(data, 'Value1'); // true

I would like to pass an array of values to look for.

2 Answers 2

2

You can use some() to check if one value from dataLookup is inside data and if it is it will return true if one isn't it will return false

var data = ['Value1','Value2','Value3'];
var dataLookup = ['Value1','Value2']

var result = dataLookup.some((e) => {return data.indexOf(e) != -1});
console.log(result)

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

5 Comments

Lodash equivalent: var result = _.every(dataLookup, _.partial(_.includes, data));
Thanks for that, however I just want to return true if one of the values from dataLookup exists within data.
@KarlBateman You can use .some() in place of .every() for that.
Thank you @NenadVracar for your time.
@JonathanLonowski Thanks for that, please post that as an answer so I may accept.
0

You can make use of difference() and equal() to check if some values from the data array exists from the dataLookup array.

var found = !_.(data).difference(dataLookup).isEqual(data);

var data = [
    'Value1',
    'Value2',
    'Value3'
];

var dataLookup = [
    'Value1',
    'Value2'
];

var found = !_(data).difference(dataLookup).isEqual(data);

console.log(found);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js"></script>

Comments

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.