[JS]Object.keys, values, entries
2021-04-12 07:25
标签:rom nbsp 使用 不同 color code ref object rop 转换对象 [JS]Object.keys, values, entries 标签:rom nbsp 使用 不同 color code ref object rop 原文地址:https://www.cnblogs.com/feng-fengfeng/p/12401413.html 1 let obj = {
2 name: "jack",
3 age: 20,
4 sex: "man",
5 }
6
7 let keys = Object.keys(obj);
8 console.log(keys); // ["name", "age", "sex"]
9
10 let values = Object.values(obj);
11 console.log(values); // ["jack", 20, "man"]
12
13 let entries = Object.entries(obj);
14 console.log(entries); // [ ["name", "jack"], ["age", 20], ["sex", "man"] ]
15 // 返回值均为数组
1 let map = new Map([
2 [‘cucumber‘, 500],
3 [‘tomatoes‘, 350],
4 [‘onion‘, 50],
5 ]);
6
7 console.log(map); // {"cucumber" => 500, "tomatoes" => 350, "onion" => 50}
8
9 // 返回可迭代项
10 let mapKeys = map.keys();
11 console.log(mapKeys); // {"cucumber", "tomatoes", "onion"}
12 console.log(Object.keys(map)); // []
13
14 let mapValues = map.values();
15 console.log(mapValues); // {500, 350, 50}
16 console.log(Object.values(map)); // []
17
18 let mapEntries = map.entries();
19 console.log(mapEntries); // {"cucumber" => 500, "tomatoes" => 350, "onion" => 50}
1 let john = Symbol("human");
2 let apple = Symbol("fruit");
3 let obj1 = {
4 [john]: "human",
5 cat: "animal",
6 [apple]: "fruit",
7 }
8 console.log(obj1); // {cat: "animal", Symbol(human): "human", Symbol(fruit): "fruit"}
9
10 let obj1Keys = Object.keys(obj1);
11 console.log(obj1Keys); // ["cat"] 会忽略 symbol 属性
12
13 let obj1Symble = Object.getOwnPropertySymbols(obj1);
14 console.log(obj1Symble); // [Symbol(human), Symbol(fruit)]
15
16 let obj1Symbles = Reflect.ownKeys(obj1);
17 console.log(obj1Symbles); // ["cat", Symbol(human), Symbol(fruit)] 返回所有键的数组
1 let prices = {
2 banana: 3,
3 orange: 2,
4 meat: 7,
5 }
6
7 let newPrices = Object.fromEntries(
8 Object.entries(prices).map(( [key, value] ) => [key, value * 2] )
9 )
10 console.log(newPrices); // {banana: 6, orange: 4, meat: 14}
上一篇:Python中的线程和进程
下一篇:文件上传绕过知识总结一
文章标题:[JS]Object.keys, values, entries
文章链接:http://soscw.com/index.php/essay/74607.html