Object(JavaScript)
宣告
{鍵 (Key) : 值 (Value)}
new Object()
資料或函式的組合=Property
透過.Key或['Key']存取或操作Object裡的property
obj[key] = value
delete obj[key]
一些原始資料型別、特殊資料型別也是Object
Boolean、Number、String、Array、Date、Function、RegExp
code:javascript
!("key" in obj)
以[key, value]的形式回傳陣列
將陣列轉換為Object
Object.fromEntries(new Map(Object.entries(obj_with_duplicated_key)))
複製
code:javascript
// Shallow Copy
// only copy first depth data
const shallowCopy = { ...obj };
// Deep Copy
// use JSON.parse/stringify
const deepCopy = JSON.parse(JSON.stringify(obj));
// or use loadsh
const deepCopy = _.cloneDeep(obj);
Although the keys of an ordinary Object are ordered now, this was not always the case, and the order is complex. As a result, it's best not to rely on property order.
The order was first defined for own properties only in ECMAScript 2015; ECMAScript 2020 defines order for inherited properties as well. But note that no single mechanism iterates all of an object's properties; the various mechanisms each include different subsets of properties. (for-in includes only enumerable string-keyed properties; Object.keys includes only own, enumerable, string-keyed properties; Object.getOwnPropertyNames includes own, string-keyed properties even if non-enumerable; Object.getOwnPropertySymbols does the same for just Symbol-keyed properties, etc.)