Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate an object into an array of Object [duplicate]

I have create one object and that object I need to pass in one method where I need to iterate by using each object. Since my Obj having only values so it its not getting passed. Can any one help me into this.

My Code :

var MyObj = {
    country : "Aus",
    Time : "EST",
    Val : "Pecific"
}

Now this MyObj I need to pass in one method:

this.someMethod(id, MyObj);

In someMethod i Have one code like

Ext.Array.forEach(MyObj, function (Value) {})

At this point it is getting failed because MyObj is not an array of object. How to correct it.

like image 982
David Avatar asked Dec 15 '22 00:12

David


1 Answers

It would be very helpful if you'd provide more information.

I am not sure what you want to achieve, but there are several ways to iterate through objects.

If you want to split up your object into multiple single-key objects:

> Object.keys(MyObj).map(key => ({ [key]: MyObj[key] }))
[ { country: 'Aus' }, { Time: 'EST' }, { Val: 'Pecific' } ]

On the other hand, if you have a function that takes an array but you want to pass just this one object:

Ext.Array.forEach([MyObj], Value => ())

(But in this case you are better off just calling the function.)

like image 187
Alex Pánek Avatar answered Jan 22 '23 17:01

Alex Pánek