2

Hi I've got follow object:

arguments = {
    familyName: undefined,
    givenName: undefined,
    age: undefined
}; 

I would like to loop each and do something with each key:value. I tried it like this:

this.arguments.forEach((item:any) => {
    if(item == undefined) {
        alert("Hallo");
    }
});

Is it possible to loop it like this? I won't a normal for or I'm not searching for such a solution, my question is if it is possible to loop it like in my forEach() above, and if it's possible how to do this. I use it often in arrays and it works, so I thought perhaps it's also possible in such an object.

Thanks

webta.st.ic
  • 4,781
  • 6
  • 48
  • 98
  • 2
    Possible duplicate of [How do I loop through or enumerate a JavaScript object?](http://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – JJJ Jul 25 '16 at 09:12
  • 2
    `forEach` is method of `Array`, not plain object – hindmost Jul 25 '16 at 09:13

3 Answers3

3

You can get keys with Object.keys the loop over it.

args = {
    familyName: undefined,
    givenName: undefined,
    age: undefined
}; 

Object.keys(args).forEach((key/*:string*/) => {
    if(args[key] === undefined) {
        console.log("Hallo");
    }
});
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
2

A forEach only works on arrays, while you are looping over an object.

Try this:

Object.keys(this.arguments).forEach((idx) => {
    var row = this.arguments[idx];
    if (row === undefined) alert('hallo');
});

You do need to be using es6 for this.

The good ol' es5 approach:

for(idx in this.arguments)
{
    if (! this.arguments.hasOwnProperty(idx)) continue;
    if (this.arguments[idx] === undefined) alert('hallo');
}
DoXicK
  • 4,784
  • 24
  • 22
2

There aren't built-in functions that help you to iterate over Objects, but, it is very easy to implement them:

Object.prototype.forEach = function(callback, context = null) {
  Object.keys(this).forEach((key, index, keys) => {
    if(!this.hasOwnProperty(key)) {
      return;
    }
    
    callback.call(context, key, this[key], index, keys);
  });
    
  return this;
}


let foo = { baz: 1, pippo: 2 };

foo.forEach((key, value, index) => console.log(index, key, value));

Some better approaches will be available with ES-NEXT:

  1. Symbol.iterator = https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator
  2. Object.entries = https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
  3. Object.values = https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Object/values
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • while i do really like your answer, it's generally a bad idea to extend/overload prototypes for native types so it's best to not promote that. Code is spot on and something i actually use myself, haha. – DoXicK Jul 25 '16 at 09:38
  • Personally, I totally agree with you... I don't like and never use prototyping built-in objects, but, having a look on the question my answer seems to be tuned. – Hitmands Jul 25 '16 at 09:47