js loop through object properties code example
Example 1: iterate over object javascript
const keys = Object.keys(fruits)
for (const key of keys) {
console.log(key)
}
Example 2: loop an object properties in ts
Object.keys(obj).forEach(e => console.log(`key=${e} value=${obj[e]}`));
Example 3: js loop over object
const object = {a: 1, b: 2, c: 3};
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
Example 4: 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 5: javascript loop through object
for (var property in object) {
if (object.hasOwnProperty(property)) {
}
}
Example 6: javascript loop through object values
var myObj = {foo: "bar", baz: "baz"};
Object.values(myObj).map((val) => {
console.log(val);
})