Object.keys, Values, Entries

JavaScript Object.keys, Values, Entries (JavaScript Object.keys、Values、Entries)

In this chapter, we are going to cover Object.keys, values, and entries. We have already spoken about methods such as map.keys(), map.values(), map.entries(). (在本章中,我们将介绍Object.keys、values和entries。 我们已经讨论过map.keys ()、map.values ()、map.entries ()等方法。)

These methods are universal and are used for data structures. Every time after creating a data structure, they should also be implemented. They are mainly used for Map, Set, and Array. (这些方法是通用的,用于数据结构。 每次创建数据结构后,还应实施它们。 它们主要用于Map、Set和Array。)

Similar methods are supported by plain objects, as well. Only the syntax differs a little. (普通对象也支持类似的方法。只是语法略有不同。)

Defining Object.keys, values, and entries

Defining Object.keys, values, and entries (定义Object.keys、值和条目)

As a rule, for plain objects, you can use the methods below:

  • For returning an array of key, the Object.keys(obj) method is used. (-对于返回键数组,使用Object.keys (obj)方法。)

  • For returning an array of values, the Object.values(obj) method is used. (-对于返回值数组,使用Object.values (obj)方法。)

  • For returning an array of [key, value] pair, the Object.entries(obj) method is used. (-对于返回[key, value]对的数组,使用Object.entries (obj)方法。)

There are significant differences compared to Map. Here we will cover them, as well. (与地图相比有显著差异。在这里,我们也将介绍它们。)

The first distinction is that here we need to call Object.keys(obj) but not obj.keys(). (第一个区别是,这里我们需要调用Object.keys (obj)而不是obj.keys ()。)

The primary reason is versatility. Objects are considered the root of all JavaScript complex structures. (主要原因是多功能性。对象被视为所有JavaScript复杂结构的根。)

So, having an object such as data, which can perform its own data.values() method. However, you may call Object.values(data) on that object, as well. Another essential difference is that Object.* methods may return real arrays, not iterabales. (因此,有一个对象,如data ,可以执行自己的data.values ()方法。 但是,您也可以对该对象调用Object.values (data)。 另一个重要的区别是Object. *方法可以返回实数组,而不是iterabales。)

Let’s consider the following example:

let user = {
 name: "Jane",
 age: 41
};
console.log(user.name); // Jane
console.log(user.age); // 41

So, in the example above, we have the following:

  • Object.keys(user) = [“name”, “age”] (- Object.keys (user) = [“name”, “age”])

  • Object.values(user) = [“Jack”, 35] (- Object.values (user) = [“Jack”, 35])

  • Object.entries(user) = [ [“name”,“Jack”], [“age”,35] ] (- Object.entries (user) = [[“name”, “Jack”], [“age” ,35]])

For looping over property values, you can use Object.values like this:

let user = {
 name: "Jane",
 age: 41
};
for (let value of Object.values(user)) {
 console.log(value); // Jane, then 41
}

Object.keys/values/entries methods have a similarity with for..in loop: both of them ignore properties that apply Symbol(…) as a key.

In circumstances when you need symbols, you can use a separate method Object.getOwnPropertySymbols, returning an array that consists of only symbolic keys. (在需要符号的情况下,可以使用单独的方法Object.getOwnPropertySymbols ,返回仅由符号键组成的数组。)

Transformation of Objects

Transformation of Objects (对象的转换)

Usually, objects don’t obtain the methods that arrays have (for instance, filter, map, and so on). (通常,对象不会获取数组具有的方法(例如,筛选器、映射等)。)

If you wish to use them, you can apply the Object.entries method, followed by Object.fromEntries. (如果要使用它们,可以应用Object.entries方法,然后应用Object.fromEntries。)

Here is the sequence of actions:

Apply Object.entries(obj) for getting an array of value/key pairs from obj. Apply array methods on the array. Apply Object.fromEntries(array) on the resulting array for turning it back to an object. (应用Object.entries (obj)从obj获取值/键对数组。 在数组上应用数组方法。 在结果数组上应用Object.fromEntries (array) ,将其转换回对象。)

Here is an example:

let points = {
 john: 22,
 ann: 20,
 jack: 14,
};
let doublePoints = Object.fromEntries(
 // convert to array, map, and then fromEntries returns an object
(//转换为array、map ,然后fromEntries返回一个对象)
 Object.entries(points).map(([key, value]) => [key, value * 2])
);
console.log(doublePoints.john); // 44

This means allows making robust transformations. (这意味着可以进行稳健的转换。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部