Common javascript array methods and object

How can I loop through all members in a JavaScript object including values that are objects.

For example, how could I loop through this (accessing the "your_name" & "your_message" for each)?

var validation_messages = "key_1": "your_name": "jimmy", "your_msg": "hello world" , "key_2": "your_name": "billy", "your_msg": "foo equals bar"


Bạn đang xem: Common javascript array methods and object

*

*

for (var key in validation_messages) // skip loop if the property is from prototype if (!validation_messages.hasOwnProperty(key)) continue; var obj = validation_messages; for (var prop in obj) // skip loop if the property is from prototype if (!obj.hasOwnProperty(prop)) continue; // your code alert(prop + " = " + obj);
*



Xem thêm: tải game mod ios

*

var obj = first: "John", last: "Doe";////Visit non-inherited enumerable keys//Object.keys(obj).forEach(function(key) console.log(key, obj););
*

The problem with this

for (var key in validation_messages) var obj = validation_messages; for (var prop in obj) alert(prop + " = " + obj); is that you’ll also loop through the primitive sầu object"s prototype.

With this one you will avoid it:

for (var key in validation_messages) if (validation_messages.hasOwnProperty(key)) var obj = validation_messages; for (var prop in obj) if (obj.hasOwnProperty(prop)) alert(prop + " = " + obj);


Xem thêm: Lập Trình Viên Php Cần Gì - Công Việc Của Lập Trình Viên Là Làm Gì

In ES6/2015 you can loop through an object lượt thích this: (using arrow function)

Object.keys(myObj).forEach(key => console.log(key); // the name of the current key. console.log(myObj); // the value of the current key.);jsbin

In ES7/2016 you can use Object.entries instead of Object.keys và loop through an object like this:

Object.entries(myObj).forEach(() => console.log(key); // the name of the current key. console.log(val); // the value of the current key.);The above would also work as a one-liner:

Object.entries(myObj).forEach(() => console.log(key, val));jsbin

In case you want lớn loop through nested objects as well, you can use a recursive function (ES6):

const loopNestedObj = obj => Object.keys(obj).forEach(key => if (obj &và typeof obj === "object") loopNestedObj(obj); // recurse. else console.log(key, obj); // or vị something with key và val. );;jsbin

Same as function above sầu, but with ES7 Object.entries() instead of Object.keys():

const loopNestedObj = obj => Object.entries(obj).forEach(() => if (val && typeof val === "object") loopNestedObj(val); // recurse. else console.log(key, val); // or vày something with key & val. );;Here we loop through nested objects change values và return a new object in one go using Object.entries() combined with Object.fromEntries() (ES10/2019):

const loopNestedObj = obj => Object.fromEntries( Object.entries(obj).map(() => if (val &và typeof val === "object") ; // recurse else ; // or vì something with key and val. ) );Another way of looping through objects is by using for ... in & for ... of. See
vdegenne"s nicely written answer.


Chuyên mục: