Associative Arrays use { } as opposed to the normal [ ]
var arr = { "one": 1, "two": 2, "three": 3 };
//to get values
for (let value of Object.values(arr)) {
alert(value);
}
//Output: 1, 2, 3
//to get keys
for (let value of Object.values(arr)) {
alert(value);
}
//Output: one, two, three
// Use a For-In Loop equine1
const equine1 = { horse: '🐴', zebra: '🦓', unicorn: '🦄'}
for (const key in equine1) {
// Filters out properties inherited from prototype, see https://palantir.github.io/tslint/rules/forin/
if (equine1.hasOwnProperty(key)) {
console.log(equine[key]);
}
}
// Unwrap the the Values
for (const val of Object.values(equine1)) {
console.log(val);
}
// Create a Map equine1
const equine2 = new Map(Object.entries(equine));
for (const v of equine1.values()) {
console.log(v)
}
MDN For In |
let names = ['Scooby-Doo', 'Shaggy Rogers', 'Fred Jones', 'Daphne Blake', 'Velma Dinkley'];
let numNames = names.length;
//initialize; test; increment
for (let i = 0; i < numNames; i++) {
console.log(names[i]);
}
** Scooby Do .js **
Saturday January 15 2022 0700 AM
Saturday December 18 2021 0822 AM
Saturday November 13 2021 0701 AM