1

I have an array of object-names

['obj','obj2','objN']

These objects are nodes in an object

{
  obj: {
    key: value,
    key2: value,
  }
  obj2:{
    key3: value
  }
...
}

I know that i for one object I can do:

for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    console.log(key)
  }
}

but is it possible to loop through all keys of multiple objects without doing any duplicates?

8
  • Loop the Array, use the Array Keys and the Initial Object Property name. Commented Sep 25, 2015 at 20:40
  • If you just care about the keys only, regardless of what happens to the value, you could merge the objects together and then iterate through the keys of that one object. Commented Sep 25, 2015 at 20:40
  • what's Initial Object Property name? Commented Sep 25, 2015 at 20:41
  • It's your ?>{ and the property names are obj:, obj2 ... Commented Sep 25, 2015 at 20:41
  • You mean loop through the objects (node names are mentioned) paralelly or consecutively? The first one requires promises the latter requires a list containing your objects. Commented Sep 25, 2015 at 20:41

2 Answers 2

1

Assuming a parent container object with the form:

var parentObj = 
{
    obj: {
        key: value,
        key2: value,
    }
    obj2: {
        key3: value
    }
    ...
}

Also if we want to include each sub-key only once, we have

var objNameArray = ['obj','obj2','objN'];
var allValues = [];
var usedKeys = {};

for(var i = 0; i < objNameArray.length; ++i){
    // If the parent object does not have a matching sub-object, skip to the
    // next iteration of the loop
    if(!parentObj[objNameArray[i]]) {
        continue;
    }
    var currentObj = parentObj[objNameArray[i]];
    var subKeys = Object.keys(currentObj);
    for(var j = 0; j < subKeys.length; ++j) {
        if(usedKeys[subKeys[j]]) {
            continue;
        }
        usedKeys[subKeys[j]] = true;
        allValues.push(currentObj[subKeys[j]]);
    }
}

All the values from all the keys in each sub-object will then be in the array allValues, and all the keys from each sub-object will be available by calling Object.keys(usedKeys);. Note that if sub-objects can also have sub-objects, this strategy needs to be adapted to support recursion.

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

Comments

0

If interpret Question correctly, try utilizing Object.keys()

var data = {
  obj: {
    key: 1,
    key2: 2,
  },
  obj2:{
    key3: 3
  }
};

for (var key in data) {
  console.log(Object.keys(data[key]))
}

2 Comments

won't this run the same keys multiple times if they exist in both obj and obj2?
@Himmators Is requirement to return only unique keys ?, or, if obj2 had key2 , do not return key2 it as part of result ? , as key2 previously iterated at obj ? What is expected result ?

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.