
Overview
In JavaScript, there are numerous ways to iterate though the properties of an object.
JavaScript Object.assign()
The Object.assign() method copies properties from one or more source objects to a target object.
// create target object
const person1 = {
firstName: "Johnny",
lastName: "Shay",
age: 12
};
// create source object
const person2 = {firstName: "Marty", lastName: "Shay"};
// assign source to target
Object.assign(person1, person2);
// display target
let text = Object.entries(person1);
document.getElementById("demo").innerHTML = text;
Output:
firstName,Marty,lastName,Shay,age,12
JavaScript Object.create()
The Object.create() method creates an object from an existing object.
// create an object:
const son = {
firstName: "Johnny",
lastName: "Shay",
age: 12
};
// create new object
const father = Object.create(son);
father.firstName = "Marty";
// display properties
document.getElementById("demo").innerHTML = son.firstName + " and " + father.firstName;
Output:
Johnny and Marty
JavaScript Object.entries()
Object.entries() returns an array of the key/value pairs in an object.
const person = {
firstName: "Johnny",
lastName: "Shay",
age: 12
};
let text = Object.entries(person); // firstName,Johnny,lastName,Shay,age,12
Object.entries() can be used to access objects in a loop.
const cars = {Lamborghini:300, Ferrari:200, Maserati:100};
let text = "";
for (let [car, amount] of Object.entries(cars)) {
text += car + ": " + amount + "k<br>";
}
document.getElementById("demo").innerHTML = text;
Output:
Lamborghini: 300k
Ferrari: 200k
Maserati: 100k
Object.entries() can be used to convert objects to maps.
const cars = {Lamborghini:300, Ferrari:200, Maserati:100};
const myMap = new Map(Object.entries(cars));
document.getElementById("demo").innerHTML = myMap; // [object Map]
JavaScript Object.fromEntries()
The fromEntries() method creates an object from a list of key/value pairs.
const cars = [
["Lamborghini", 300],
["Ferrari", 200],
["Maserati", 100]
];
const myObj = Object.fromEntries(cars);
document.getElementById("demo").innerHTML = myObj.Maserati + "k";
Output:
100k
JavaScript Object.keys()
The Object.keys() method returns an array with the keys of an object.
// create an object
const person = {
firstName: "Johnny",
lastName: "Shay",
age: 12,
eyeColor: "brown"
};
// get the keys
const keys = Object.keys(person);
document.getElementById("demo").innerHTML = keys;
Output:
firstName,lastName,age,eyeColor
JavaScript Object.values()
Object.values() is similar to Object.entries(), but returns a single dimension array of the object values.
const cars = [
["Lamborghini", 300],
["Ferrari", 200],
["Maserati", 100]
];
let text = Object.values(cars)
document.getElementById("demo").innerHTML = text
Output:
Lamborghini,300,Ferrari,200,Maserati,100
JavaScript Object.groupBy()
The Object.groupBy() method groups elements of an object according to string values returned from a callback function. It not change the original object.
// create an array
const cars = [
{name:"Lamborghini", quantity:300},
{name:"Ferrari", quantity:200},
{name:"Maserati", quantity:100},
{name:"Alfa Romeo", quantity:75}
];
// callback function to select high prices
function myCallback({ quantity }) {
return quantity > 100 ? "high" : "low";
}
// group by ok and low
const result = Object.groupBy(cars, myCallback);
// display results
let text ="These cars are low priced: <br>";
for (let [x,y] of result.low.entries()) {
text += y.name + " " + y.quantity + "k<br>";
}
text += "<br>These cars are high priced: <br>";
for (let [x,y] of result.high.entries()) {
text += y.name + " " + y.quantity + "k<br>";
}
document.getElementById("demo").innerHTML = text;
Output:
These cars are low priced:
Maserati 100k
Alfa Romeo 75k
These cars are high priced:
Lamborghini 300k
Ferrari 200k
JavaScript for…in Loop
The JavaScript for…in statement loops through the properties of an object.
const person = {
fname: "Johnny",
lname: "Shay",
age: 12
};
let txt = "";
for (let x in person) {
txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
Output:
Johnny Shay 12
JavaScript Notes:
- When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
- JavaScript is a case-sensitive language; firstName is NOT the same as firstname
- JavaScript variables are case sensitive (x is not the same as X)
- Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
- JavaScript variables must begin with a letter, $, or _
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.
