JavaScript Object Iterations

Back to JS page

Table of Contents

JavaScript Object.entries()

Object.entries() returns an array of the key/value pairs of an object (as arrays):

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Iterations</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Object.entries()</h4>
<p id="demo"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

const entries = Object.entries(person);
let text = "";
for (let entry of entries) {
  text += entry[0] + ": " + entry[1] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 1

Result View Example

JavaScript Object.values()

Object.values() returns an array of the property values of an object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Iterations</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Object.values()</h4>
<p id="demo"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

const values = Object.values(person);
document.getElementById("demo").innerHTML = "Values: " + values;
</script>

</body>
</html>

Example 2

Result View Example

JavaScript Object.keys()

Object.keys() returns an array of the property keys (names) of an object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Iterations</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Object.keys()</h4>
<p id="demo"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

const keys = Object.keys(person);
let text = "";
for (let key of keys) {
  text += key + "<br>";
}
document.getElementById("demo").innerHTML = "Keys:<br>" + text;
</script>

</body>
</html>

Example 3

Result View Example

JavaScript for...in Loop

The for...in loop iterates over the properties of an object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Iterations</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>for...in Loop</h4>
<p id="demo"></p>

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

let text = "";
for (let key in person) {
  text += key + ": " + person[key] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 4

Result View Example

JavaScript Object.groupBy()

Object.groupBy() groups elements of an array by a string value returned from a callback function:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Iterations</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Object.groupBy()</h4>
<p id="demo"></p>

<script>
const fruits = [
  { name: "apple", color: "red" },
  { name: "banana", color: "yellow" },
  { name: "grape", color: "purple" },
  { name: "strawberry", color: "red" }
];

// Group fruits by color
const result = Object.groupBy(fruits, item => item.color);

let text = "";
for (let color in result) {
  text += color + ": " + result[color].map(f => f.name).join(", ") + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference