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.)
定義為unordered,不保證順序
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software. Implementations whose behavior does not depend on member ordering will be interoperable in the sense that they will not be affected by these differences.
JavaScript
數字>property建立順序>symbol建立順序
9.4.3.3 [[OwnPropertyKeys]] ()
JSON.parse()
不保證key順序,由函式庫決定實作