Iterate Through Object Properties

How do I loop through or enumerate a JavaScript object?

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {

"p1": "value1",

"p2": "value2",

"p3": "value3"

};

for (var key in p) {

if (p.hasOwnProperty(key)) {

console.log(key + " -> " + p[key]);

}

}

Iterate over object properties in JSX

as you got the answer in the comment you have to use the map which will return the array of children that will be rendered in the jsx.

here is the code snippet.

/* other react component body
.
.
.
*/

// targeted object
var currObj = {
SKU : 'Test_SKU_120934',
category : 'Home',
inventory_status : 1.
}

const objectList = Object.keys(currObj).map(k => (
<li
key={k}
><strong>{k}</strong>: {currObj[k]}</li>
));

return (
<ul>{objectList}</ul>
);

// here objectList is returned by the map function. and Object.keys function creates iterable array of keys from object.

How does one iterate through an object's property values in order to verify for some values (objects as well) each another specific property value?

Here's one way to look for a specific id. I consoled the object found and the length of that specific object. But I am being very unclear with what exactly you want.

let formik = {
"randomObj": 'something',
"values": {
flight1: {
'name': "flight1",
'timecreated': "sometime",
id: 548497984
},
flight2: {
'name': "flight2",
'timecreated': "sometime",
id: 548497982
},
flight3: {
'name': "flight3",
'timecreated': "sometime",
id: 548497989
},
flight4: {
'name': "flight4",
'timecreated': "sometime",
id: 548497981
}
},
attributes: 'null'
}

for (let key in formik.values) {
if (formik.values[key].id === 548497984) {
console.log(formik.values[key])
console.log("Length is: ", Object.keys(formik.values[key]).length)
break;
} else {
console.log("no matches")
}
}

How to iterate over a JavaScript object?

For iterating on keys of Arrays, Strings, or Objects, use for .. in :

for (let key in yourobject) {
console.log(key, yourobject[key]);
}

With ES6, if you need both keys and values simultaneously, do

for (let [key, value] of Object.entries(yourobject)) {
console.log(key, value);
}

To avoid logging inherited properties, check with hasOwnProperty :

for (let key in yourobject) {
if (yourobject.hasOwnProperty(key)) {
console.log(key, yourobject[key]);
}
}

You don't need to check hasOwnProperty when iterating on keys if you're using a simple object (for example one you made yourself with {}).

This MDN documentation explains more generally how to deal with objects and their properties.

If you want to do it "in chunks", the best is to extract the keys in an array. As the order isn't guaranteed, this is the proper way. In modern browsers, you can use

let keys = Object.keys(yourobject);

To be more compatible, you'd better do this :

 let keys = [];
for (let key in yourobject) {
if (yourobject.hasOwnProperty(key)) keys.push(key);
}

Then you can iterate on your properties by index: yourobject[keys[i]] :

for (let i=300; i < keys.length && i < 600; i++) { 
console.log(keys[i], yourobject[keys[i]]);
}

How to iterate over object properties?

var customer = {

FirstName: "",

LastName: "MyLastName",

Email: "",

};

for(var propertyName in customer) {

var propertyValue = customer[propertyName];

console.log('LIST PROPERTIES: ', propertyName, propertyValue);



if(propertyName == "LastName" && propertyValue == "MyLastName") {

// do something

console.log('your ' + propertyName + ' is ' + propertyValue);

}

}

Looping through object properties, one of which is an array (JS)

This will loop through each key (e.g. color, make, etc.) of your car object, and print the key and the value.

Object.keys(car).forEach(key => {
console.log(key, car[key]);
});

Will output the following:

color red
make Chevrolet
model Camaro
year 1967
options [ 'hard top', 'power windows', 'racing stripe', 'fog lights' ]

Iterating through Object Properties in C#

public IEnumerator<PropertyInfo> GetEnumerator()
{
return GetEnumerator();
}

This method is calling itself, eventually your stack will overflow. Implement it properly:

public IEnumerator<PropertyInfo> GetEnumerator()
{
foreach (var property in typeof(Customer).GetProperties())
{
yield return property;
}
}

How do I loop through object properties minus the last value pair in javascript?

So as charlietfl said the reason is that for(const key of keys.length - 1) is like writing for(const key of 5), that is not how for of loops work.

What I would suggest doing is:

var obj = {
a: "foo",
b: "bar",
c: "foobar",
d: "something1",
e: "something2",
f: "something3"
};

const keys = Object.keys(obj)
for (var i = 0; i < keys.length - 1; i++) {
console.log(obj[keys[i]])
}

So what we do here is we do a normal for (var i = 0; i < length - 1; i++)

Here our length is keys.length - 1 which is 5, so we are going to skip the last one like you want to do.

Then log the obj keys value console.log(obj[keys[i]).

Hopefully this is helpful, I have also made a snippet so you can see it in action.

var obj = {

a: "foo",

b: "bar",

c: "foobar",

d: "something1",

e: "something2",

f: "something3"

};

const keys = Object.keys(obj)

for (var i = 0; i < keys.length - 1; i++) {

console.log(obj[keys[i]])

}

Iterating through object properties and setting nullable properties through user input

Use Nullable.GetUnderlyingType() to get the underlying type of a nullable type. If it's not a nullable type, it returns null, so you can check for null and use the actual property type instead. Like this:

var convertedInput = Convert.ChangeType(input,
Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);


Related Topics



Leave a reply



Submit