2

Heres the situation -

I have an Array and an Object.

say:

var Array = ["field1", "field2", "field3"];
var Object = { field1: undefined, field2: undefined, field3: undefined, field4: undefined, field5: undefined}

*the values in the object don't really matter right now

Using underscoreJS i want to iterate over the Object and check to see if each of the keys exists in the Array array. If it does i want to be able to set its value to true and if not i want to set it to false.

So far i've tried:

 _.mapObject(Object, function(val, key){

        for (var j = 0; j < Array.length; j++) {
            currentKey = Array[j];
            (function(i){
                for (var k = 0; k < Array.length; k++) {
                    if(key === Array[i]) {
                        return val = true;
                    } else {
                        return val = false;
                    }
                }
            })(currentKey);
        }

    });

Shit is very confusing; for me at least. Any help is appreciatated.

2
  • 3
    It's a very bad idea to name your objects Object and your arrays Array, as that are the built-in class* names and you're likely to get lots of funny errors later. Instead, how about obj and arr? Commented May 5, 2016 at 17:23
  • You must not return false if only one of the keys is not the same. Commented May 5, 2016 at 17:24

4 Answers 4

1

Here's a solution using underscore:

    var result = _.mapObject(Object, function(value, field){
        return _.contains(Array, field);
    });

or if you want to change Object itself:

    var result = _.each(Object, function(value, field){
        return _.contains(Array, field);
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much @Gruff bunny!
1

I don't think you need underscore for this:

var a = ["field1", "field2", "field3"];
var o = { field1: undefined, field2: undefined, field3: undefined, field4: undefined, field5: undefined}

for (var k in o) {
  o[k] = a.indexOf(k) != -1;
}

Comments

1

As others have said there's no need for underscore.

You can use the native Object.keys to return an array of own keys then iterate over them. Object.keys will not include inherited properties from the prototype chain which for in will. It is however considerably slower which could be a problem with large data-sets.

Object.keys(obj)
    .forEach(function (key) {
        obj[key] = (arr.indexOf(key) !== -1);
    });

Fiddle

Object.keys

Comments

0

You don't need underscore to do this:

var arr = ['a','b','c'],
        obj = {a: '', b: '', c: '', d:''};

function check (a, o) {
    for (var key in o) {
        if (a.indexOf(key) != -1) {
            o[key] = true
        }
    }
}

check(arr,obj);

document.write(JSON.stringify(obj))

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.