javascript
  1. javascript-entries

JavaScript entries()

The entries() method is a built-in method in JavaScript that is used to return an array of key-value pairs from an object.

Syntax

The syntax for the entries() method is as follows:

object.entries()

Here, object is the object from which you want to retrieve the key-value pairs.

Example

Consider the following object:

const car = {
    make: 'Honda',
    model: 'Civic',
    year: 2021
};

To get the key-value pairs from this object, you can use the entries() method as follows:

const entries = Object.entries(car);
console.log(entries);

This will output:

[ [ 'make', 'Honda' ], [ 'model', 'Civic' ], [ 'year', 2021 ] ]

Output

The entries() method returns an array of arrays. Each nested array contains two elements, the first element being the key and the second element being the corresponding value.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Car Object Example</title>
</head>
<body>

<!-- Display area for the output -->
<div id="output"></div>

<script>
    // Your JavaScript code
    const car = {
        make: 'Honda',
        model: 'Civic',
        year: 2021
    };

    // Get the key-value pairs from the object
    const entries = Object.entries(car);

    // Display the output in the HTML document
    let outputHtml = '<ul>';
    for (const [key, value] of entries) {
        outputHtml += `<li>${key}: ${value}</li>`;
    }
    outputHtml += '</ul>';

    document.getElementById('output').innerHTML = outputHtml;
</script>

</body>
</html>
Try Playground

Explanation

The entries() method returns a new array that contains an array of key-value pairs for each property in the object. The order of the properties in the returned array are the same as that provided by a for...in loop except that the returned array is not bound to any object.

Use

The entries() method is useful in situations where you need to extract key-value pairs from an object and represent them in a different format. It is commonly used in converting objects to Maps and in converting objects to arrays of key-value pairs.

Important Points

  • The returned array from the entries() method is not bound to the original object, which means that you can modify the array without affecting the original object.
  • The order of the properties in the returned array is not guaranteed to be the same as the order of keys provided by the for...in loop.
  • The method only returns enumerable properties in the object.

Summary

The entries() method is a robust and essential built-in method in JavaScript that allows you to extract key-value pairs from an object and represent them in a new array. It is particularly useful when you need to convert objects to maps or arrays of key-value pairs and is relatively simple to use. The method returns an array of arrays, and each nested array contains a key-value pair.

Published on: