0

Given array parameter ary ["value", "value2"]

I want to loop through an object of objects to find a match for both array values. I've tried as such:

function findMatch (ary) {

  storageArray = [];
  mykeys = [];
  myvalues = [];

  for (var i = 0; i < ary.length; i++) {
    for (obj in object) {
      for (key in object[obj]) {
        if (ary[i] in object[obj]) {
           mykeys.push(key);
           myvalues.push(object[obj][ary[i]]);
        }
      }
    }
   storageArray .push(mykeys, myvalues);
   return storageArray;
}

var object = {
    "subobject" :
    {
        'key'   : 'value',
        'key2'  : 'value2',
        'key3'  : 'value3'      
    },
    "subobject2" : 
    {
        'key4'  : 'value4'

    },
}

Goal: be able to return the k : v pair for each match of a value in ary

meaning... since ary has value and value2... I want to return a match from the object of key : value and key2 : value2.

So far this a) isn't working and b) doesn't seem like the most efficient way to do it (3 for loops...)

3
  • I'm not quite following...Do you mean to return [{"key":"value"},{"key2":"value2"}]? And what if object has another item "subobject3:{"key":"value"}? Commented Feb 6, 2014 at 4:48
  • @Passerby yes that's what I mean. And what's wrong if it has another term? Commented Feb 6, 2014 at 5:11
  • 1
    Because my example subobject3 has only one match ("value"). What's the expected output then? Commented Feb 6, 2014 at 5:35

1 Answer 1

1

It is a similar way as you did, and it works.

function findMatch(array, object) {
    var result = [];
    for (var sub in object) {
        var subObj = object[sub];
        for (var key in subObj) {
            for (var i = 0; i < array.length; i++) {
                if (subObj[key] === array[i]) {
                    result.push({
                        k: key,
                        v: subObj[key]
                    });
                }
            }
        }
    }
    return result;
}

var result = findMatch(['value', 'value2'], object);
for (var i = 0; i < result.length; i++) {
    console.log(result[i].k + " : " + result[i].v);
}

Here is another way using a lookup object:

function findMatch(array, object) {
    var result = [];
    var lookup = {};
    for (var sub in object) {
        var subObj = object[sub];
        for (var key in subObj) {
            var value = subObj[key];
            if (!lookup[value]) {
                lookup[value] = [];
            }
            lookup[value].push(key);
        }
    }
    for (var i = 0; i < array.length; i++) {
        var val = array[i];
        var foundArr = lookup[val];
        if (foundArr) {
            for (var j = 0; j < foundArr.length; j++) {
                result.push({
                    k: foundArr[j],
                    v: val
                });
            }
        }
    }
    return result;
}

var result = findMatch(['value', 'value2'], object);
for (var i = 0; i < result.length; i++) {
    console.log(result[i].k + " : " + result[i].v);
}
Sign up to request clarification or add additional context in comments.

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.