使用数组,您通常想要实现一定数量的特定目标。下面是您想要对对象执行的几乎所有操作的列表,以及如何在 Javascript 中执行此操作。
1. 更新对象的属性#
使用=
运算符:
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } // Updates myAge to 2043 object.myAge = 2043
2. 将对象的键转换为数组#
使用keys()
:
let object = { 'myName' : 'Name', 'myAge' : 1043 } // Returns [ 'myName', 'myAge' ]; let keys = Object.keys(object);
3. 将对象的值转换为数组#
使用values()
:
let object = { 'myName' : 'Name', 'myAge' : 1043 } // Returns [ 'Name', 1043 ]; let values = Object.values(object);
4. 将 Array 或 Map 集转换为 Object#
使用fromEntries
:
let arrSets = [ ['myName', 'Name'], ['myAge', 1043] ] /* Returns { 'myName' : 'Name', 'myAge' : 1043 } */ let generateObject = Object.fromEntries(arrSets);
5. 浅克隆对象#
使用assign()
或...
:
let object = { 'myName' : 'Name', 'myAge' : 1043 } // Creates a copy of object, which we can edit separately let newObject = Object.assign({}, object); // Creates a copy of object, which we can edit separately let anotherClone = { ...object };
6. 深度克隆一个只有变量的对象#
使用JSON.parse(JSON.stringify())
:
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } // Creates a copy of object, which we can edit separately let newObject = JSON.parse(JSON.stringify(object)); newObject.myName.FirstName = 'Hello'; console.log(newObject, object); /* Returns { myAge: 1043, myName: { FirstName: "Hello", SecondName: "Surname" } }, { myAge: 1043, myName: { FirstName: "Name", SecondName: "Surname" } } */
7.将两个对象合并到原始变量中#
使用assign()
:
let object = { 'myName' : 'Name' } let objectTwo = { 'myAge' : 1043 } Object.assign(object, objectTwo); console.log(object, objectTwo); /* Returns { myAge: 1043, myName: "Name" }, { myAge: 1043 } */
8. 将两个对象合并成一个新变量#
使用...
.
let object = { 'myName' : 'Name' } let objectTwo = { 'myAge' : 1043 } let newObject = { ...object, ...objectTwo } console.log(object, newObject); /* Returns { myName: "Name" }, { myName: "Name", myAge: 1043 } */
注意:如果你用 合并两个对象...
,并且有重复的键(即两者都有myAge),第二个对象将覆盖第一个。
9. 防止向对象添加新项目,但允许更改以前的项目#
使用preventExtensions()
:
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } Object.preventExtensions(object); // Throws a TypeError object.myLocation = '123 Fake Street';
10.防止对对象进行任何更改#
使用freeze()
:
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } Object.freeze(object); // Throws a TypeError object.myLocation = '123 Fake Street'; // Throws a TypeError object.myAge = 2043
11.将对象变成字符串#
使用JSON.stringify()
:
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } // Returns {"myName":{"FirstName":"Name","SecondName":"Surname"},"myAge":1043} console.log(JSON.stringify(object))
12. 将字符串变成对象#
使用JSON.parse()
:
let stringObject = '{"myName":{"FirstName":"Name","SecondName":"Surname"},"myAge":1043}'; /* Returns { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } */ console.log(JSON.parse(object))
13.检查Object是否有属性#
使用hasOwnProperty()
:
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } // Returns true console.log(object.hasOwnProperty('myName'))
14. 使对象的属性不可写,这样你就无法更改它#
使用defineProperty()
和更改writable
:
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } Object.defineProperty(object, 'myAge', { writable: false, }); // object.myAge remains 1043 object.myAge = 2043;
15. 使用 for 循环时忽略某些属性#
使用defineProperty()
和改变enumerable
。如果我们将 enumerable 设置为 false,则该项目将在forEach
循环中被忽略。
let object = { 'myName' : { 'FirstName' : 'Name', 'SecondName' : 'Surname' }, 'myAge' : 1043 } Object.defineProperty(object, 'myAge', { enumerable: false, }); // Returns only 'myAge' Object.keys(object).forEach(function(item) { console.log(item); });
16. 将对象转换为数组集#
使用entries()
:
let object = { 'myName' : 'Name', 'myAge' : 1043 } // Returns [ [ 'myName', 'Name' ], [ 'myAge', 1043 ]]; let entries = Object.entries(object);