Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through all the fields of an object

I have an object that has about 23 columns. Is there a way to iterate through each column automatically? Rather than specifically selecting each column using .get("COLUMN_NAME") ?

Thanks guys.

like image 433
luigisrs Avatar asked Oct 27 '25 08:10

luigisrs


1 Answers

That's say a Class A -- with fields' id, createdAt, updatedAt, a, b, c and obj is an instance of A.

obj.attributes is an object which hold a, b, c and id, createdAt, updateAt are properties of obj.

The following is an example to show all fields' name except special field (id, createdAt, updatedAt) in web console.

Object.keys(obj.attributes).forEach(function(fieldName) {
    console.log(fieldName);
});
like image 146
eth3lbert Avatar answered Oct 30 '25 14:10

eth3lbert