Работа с объектами в JavaScript: использование метода Object.entries()

JS (JavaScript) Object.entries()

Object.entries() is a method in JavaScript that allows us to get an array of an object's own enumerable properties in the form of [key, value] pairs. This method was added in the ECMAScript 2017 standard and allows us to efficiently manipulate and iterate over object properties.

Let's take a look at some code examples to better understand how to use Object.entries().

Example 1:

<pre data-lang="javascript">
const person = {
  name: 'John',
  age: 30,
  city: 'New York'
};

const entries = Object.entries(person);

console.log(entries);
</pre>

In this example, we create an object person with several properties such as name, age, and city. Then, we apply the Object.entries() method to the person object and save the result in the entries variable. After that, we output entries to the console.

The output result will be as follows:

[
  ['name', 'John'],
  ['age', 30],
  ['city', 'New York']
]

We get an array consisting of [key, value] arrays, where each inner array contains a property and its value from the person object.

Example 2:

<pre data-lang="javascript">
const studentGrades = {
  Math: 90,
  Science: 85,
  History: 95
};

for (const [subject, grade] of Object.entries(studentGrades)) {
  console.log(`The grade in ${subject} is ${grade}`);
}
</pre>

In this example, we have an object studentGrades that contains subjects and student grades. We use a for...of loop and destructuring assignment to unpack the [key, value] pairs returned by the Object.entries() method. Then, we output a message with the subject and the student's grade to the console.

The output result will be as follows:

The grade in Math is 90
The grade in Science is 85
The grade in History is 95

We iterate over each array element, unpacking it into the variables subject and grade, and output the corresponding messages based on these values.

Object.entries() can also be used to convert an object into a Map. For example:

<pre data-lang="javascript">
const person = {
  name: 'Alice',
  age: 25,
  city: 'London'
};

const personMap = new Map(Object.entries(person));

console.log(personMap);
</pre>

In this example, we use Object.entries() for the person object and pass it to the Map constructor, creating an instance personMap. Then, we output personMap to the console.

The output result will be as follows:

Map(3) {
  'name' => 'Alice',
  'age' => 25,
  'city' => 'London'
}

Now we have a Map containing the keys and values from the person object.

In conclusion, using the Object.entries() method allows us to access object properties in the form of [key, value] arrays, making it easier to manipulate and iterate over properties. This method is useful when needing to iterate through properties or convert an object to a Map.

Похожие вопросы на: "js object entries "

Padding CSS: примеры использования и настройки
Steam API: интеграция, функции, документация
Локальный сервер PHPMyAdmin
<h1>jQuery each: Как использовать цикл each в jQuery
Сортировка массива: алгоритмы и методы викторины
Useref: использование useRef в React
Python ASCII: генератор символьной графики и кодирования
Изучение CSS Select
Не удалось найти пакет - Решение проблемы
Microsoft Visual C++ Runtime Ошибка: причины и способы решения