How to iterate an object into an object array

I created one object and the object that I need to pass in one method, where I need to iterate through each object. Since my Obj only matters, so it fails. Can anyone help me on this.

My code is:

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

Now I need to transfer this MyObj in one way:

this.someMethod(id, MyObj);

In someMethod I have one code like

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

At this point, it is rejected because it is MyObjnot an array of the object. How to fix it.

+4
source share
4 answers

ExtJs Method

ExtJs provides Ext.Object.eachValuewhat you are looking for.
From ExtJs Documentation :

. , false .

MyObj .

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

Ext.Object.eachValue(MyObj, function (Value) {console.log(Value)});
+4

, .

, , .

:

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

, , , :

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

( .)

+3

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

//Without ext
function someMethod(id, MyObj)
{
  Object.keys(MyObj).forEach(function (Value) {
console.log(MyObj[Value]);
});
}

someMethod(1, MyObj);
Hide result

(vanilla JS) Object Object.keys . .

+3

:

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

function someFunction(id, obj){
  var objArray = $.map(obj, function(el) { 
    console.log(el);
    return el 
  });
}
someFunction(1, MyObj)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hide result
+3

Source: https://habr.com/ru/post/1672233/


All Articles