Example 1: javascript loop object
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
for (let key in obj) {
let value = obj[key];
console.log(key, value);
}
Example 2: javascript loop through object example
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
Example 3: loop an object properties in ts
Object.keys(obj).forEach(e => console.log(`key=${e} value=${obj[e]}`));
Example 4: js loop through object
const obj = { a: 1, b: 2 };
Object.keys(obj).forEach(key => {
console.log("key: ", key);
console.log("Value: ", obj[key]);
} );
Example 5: javascript loop through object
Object.entries(obj).forEach(
([key, value]) => console.log(key, value)
);
arr.forEach((item, index) => {
})
for (const item of arr) {
await something()
}
const arr = [1,2,3]
const newArray = arr.map(item => item * 2)
console.log(newArray)
const arr = [1,2,3,1]
const newArray = arr.filter(item => {
if (item === 1) { return item }
})
console.log(newArray)
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const arr3 = [...arr1, ...arr2]
console.log(arr3)
const { email, address } = user
console.log(email, address)
const obj = { name: 'my name' }
const clone = { ...obj }
console.log(obj === clone)
Example 6: javascript loop through object
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}