2

I have 2 arrays :

labels = ["water", "sever", "electricity"];
services = [0:[0:"1",1:"sever"], 1:[0:"3",1:"park"], 3:[0:"4",1:"gas"]];

I want to check if ANY of labels value is in services or not (True or False).

What is the best way to archive this using jquery?

EDIT 1:

Yes, I made a mistake while asking question, services is a JSON Object.

UPDATE

Bregi's 2nd solution is what I needed. .some is fulfilling purpose.

var isInAny = labels.some(function(label) {
  return services.some(function(s) {
    return s[1] == label;
  });
});
3
  • 2
    This is not valid JavaScript syntax Commented Sep 19, 2013 at 20:14
  • jQuery does not help here. Check out the Underscore.js library. Commented Sep 19, 2013 at 20:14
  • services looks like an object to me. Commented Sep 19, 2013 at 20:14

3 Answers 3

1

Use the some Array method (though you might need to shim it for legacy environments):

var isInAny = labels.some(function(label) {
    // since we don't know whether your services object is an actual array
    // (your syntax is invalid) we need to choose iteration/enumeration
    if (typeof services.length == "number") {
        for (var i=0; i<services.length; i++)
            if (services[i][1] == label)
                return true;
    } else {
        for (var p in services)
            if (services[p][1] == label)
                return true;
    }
    return false;
});

If services is really an Array, you could also use

var isInAny = labels.some(function(label) {
    return services.some(function(s) {
        return s[1] == label;
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

services is an Array and your 2nd solution worked perfectly..
1

Assuming those should be braces instead of brackets in services

labels.some(function (label) {
    for (var x in services) {
        if (services.hasOwnProperty(x)) {
            return services[x][1] === label;
        }
    }
});

http://jsfiddle.net/WwsLk/

4 Comments

services is JSON created from database. I am not sure why you said braces instead of brackets in services. It will be helpful if you can give some more information about it. and Yes .some is perfectly working as I needed but with some changes in your code (@Bergi's Solution is giving proper results in my case).
@anand bracket: [, brace: {. What changes?
I am using above code with brackets ([) only.
@anand I understand now. services in your question is not valid JS; just make sure to make it valid for your next question :)
1

This has often been answered here. You may want to have a look ^^

But, to give you an answer: Use $.each on services to generate a new array with the strings from there and compare this to the labels array.

This should get you an idea: Compare two multidimensional arrays in javascript

2 Comments

That question has the same title, but a very different topic.
-1 for commenting without reading complete question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.