Phương thức Đối tượng mà các thuộc tính của chính có thể được trả lại. Một mảng các chuỗi đại diện cho tất cả các thuộc tính có thể suy nghĩ của đối tượng đã cho. Object.keys[]
trả về một mảng của một tên thuộc tính có thể tìm thấy của một đối tượng nhất định, được lặp lại theo cùng một thứ tự mà một vòng lặp bình thường sẽ làm.Object.keys[]
method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would. Thử nó
Cú pháp
Thông số
obj
Giá trị trả về
Sự mô tả
Object.keys[]
Trả về một mảng có các phần tử là các chuỗi tương ứng với các thuộc tính được tìm thấy trực tiếp trên object
. Thứ tự của các thuộc tính giống như được đưa ra bằng cách lặp qua các thuộc tính của đối tượng theo cách thủ công.Ví dụ
Sử dụng object.keys
// simple array
const arr = ['a', 'b', 'c'];
console.log[Object.keys[arr]]; // console: ['0', '1', '2']
// array-like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.keys[obj]]; // console: ['0', '1', '2']
// array-like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.keys[anObj]]; // console: ['2', '7', '100']
// getFoo is a property which isn't enumerable
const myObj = Object.create[{}, {
getFoo: {
value[] { return this.foo; }
}
}];
myObj.foo = 1;
console.log[Object.keys[myObj]]; // console: ['foo']
Nếu bạn muốn tất cả các thuộc tính, bao gồm cả những người không kích thích, xem Object.getOwnPropertyNames[]
.
Cưỡng chế không đối tượng
Trong ES5, nếu đối số của phương pháp này không phải là một đối tượng [nguyên thủy], thì nó sẽ gây ra ____10.
Từ ES2015 trở đi, một đối số không phải đối tượng sẽ bị ép buộc đến một đối tượng.
// In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
Thông số kỹ thuật
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-object.keys # sec-object.keys |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt
Xem thêm
Phương thức
1 trả về một mảng của một thuộc tính được ghi theo chuỗi của một đối tượng đã cho // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
2. Điều này giống như lặp lại với vòng lặp // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
3, ngoại trừ vòng lặp // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
3 cũng liệt kê các thuộc tính trong chuỗi nguyên mẫu.// In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
1 method returns an array of a given object's own enumerable string-keyed property // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
2 pairs. This is the same as iterating with a // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
3 loop, except that a // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
3 loop enumerates properties in the prototype chain as well. // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
Thứ tự của mảng được trả về bởi
// In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
1 giống như được quy định bởi vòng lặp // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
3. Nếu có nhu cầu đặt hàng khác nhau, thì mảng sẽ được sắp xếp trước, như // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
7.Thử nó
Cú pháp
Thông số
obj
Đối tượng có các cặp được mã hóa chuỗi riêng
// In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
2 sẽ được trả về.Giá trị trả về
Một mảng của thuộc tính chuỗi được định sẵn của đối tượng đã cho
// In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
2 cặp.Sự mô tả
// In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
1 Trả về một mảng có các phần tử là các mảng tương ứng với thuộc tính được khóa chuỗi enumable // In ES5
Object.keys['foo']; // TypeError: "foo" is not an object
// In ES2015+
Object.keys['foo']; // ["0", "1", "2"]
2 được tìm thấy trực tiếp trên object
. Thứ tự của các thuộc tính giống như được đưa ra bằng cách lặp qua các giá trị thuộc tính của đối tượng theo cách thủ công.Ví dụ
const obj = { foo: 'bar', baz: 42 };
console.log[Object.entries[obj]]; // [ ['foo', 'bar'], ['baz', 42] ]
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.entries[obj]]; // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.entries[anObj]]; // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
// getFoo is property which isn't enumerable
const myObj = Object.create[{}, { getFoo: { value[] { return this.foo; } } }];
myObj.foo = 'bar';
console.log[Object.entries[myObj]]; // [ ['foo', 'bar'] ]
// non-object argument will be coerced to an object
console.log[Object.entries['foo']]; // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
// returns an empty array for any primitive type except for strings [see the above example], since primitives have no own properties
console.log[Object.entries[100]]; // [ ]
// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for [const [key, value] of Object.entries[obj]] {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}
// Or, using array extras
Object.entries[obj].forEach[[[key, value]] => {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}];
Chuyển đổi một đối tượng thành bản đồ
Chất xây dựng
const obj = { foo: 'bar', baz: 42 };
console.log[Object.entries[obj]]; // [ ['foo', 'bar'], ['baz', 42] ]
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.entries[obj]]; // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.entries[anObj]]; // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
// getFoo is property which isn't enumerable
const myObj = Object.create[{}, { getFoo: { value[] { return this.foo; } } }];
myObj.foo = 'bar';
console.log[Object.entries[myObj]]; // [ ['foo', 'bar'] ]
// non-object argument will be coerced to an object
console.log[Object.entries['foo']]; // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
// returns an empty array for any primitive type except for strings [see the above example], since primitives have no own properties
console.log[Object.entries[100]]; // [ ]
// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for [const [key, value] of Object.entries[obj]] {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}
// Or, using array extras
Object.entries[obj].forEach[[[key, value]] => {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}];
4 chấp nhận một const obj = { foo: 'bar', baz: 42 };
console.log[Object.entries[obj]]; // [ ['foo', 'bar'], ['baz', 42] ]
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.entries[obj]]; // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.entries[anObj]]; // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
// getFoo is property which isn't enumerable
const myObj = Object.create[{}, { getFoo: { value[] { return this.foo; } } }];
myObj.foo = 'bar';
console.log[Object.entries[myObj]]; // [ ['foo', 'bar'] ]
// non-object argument will be coerced to an object
console.log[Object.entries['foo']]; // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
// returns an empty array for any primitive type except for strings [see the above example], since primitives have no own properties
console.log[Object.entries[100]]; // [ ]
// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for [const [key, value] of Object.entries[obj]] {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}
// Or, using array extras
Object.entries[obj].forEach[[[key, value]] => {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}];
5 có thể điều chỉnh được. Với const obj = { foo: 'bar', baz: 42 };
console.log[Object.entries[obj]]; // [ ['foo', 'bar'], ['baz', 42] ]
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.entries[obj]]; // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.entries[anObj]]; // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
// getFoo is property which isn't enumerable
const myObj = Object.create[{}, { getFoo: { value[] { return this.foo; } } }];
myObj.foo = 'bar';
console.log[Object.entries[myObj]]; // [ ['foo', 'bar'] ]
// non-object argument will be coerced to an object
console.log[Object.entries['foo']]; // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
// returns an empty array for any primitive type except for strings [see the above example], since primitives have no own properties
console.log[Object.entries[100]]; // [ ]
// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for [const [key, value] of Object.entries[obj]] {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}
// Or, using array extras
Object.entries[obj].forEach[[[key, value]] => {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}];
6, bạn có thể dễ dàng chuyển đổi từ const obj = { foo: 'bar', baz: 42 };
console.log[Object.entries[obj]]; // [ ['foo', 'bar'], ['baz', 42] ]
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.entries[obj]]; // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.entries[anObj]]; // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
// getFoo is property which isn't enumerable
const myObj = Object.create[{}, { getFoo: { value[] { return this.foo; } } }];
myObj.foo = 'bar';
console.log[Object.entries[myObj]]; // [ ['foo', 'bar'] ]
// non-object argument will be coerced to an object
console.log[Object.entries['foo']]; // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
// returns an empty array for any primitive type except for strings [see the above example], since primitives have no own properties
console.log[Object.entries[100]]; // [ ]
// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for [const [key, value] of Object.entries[obj]] {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}
// Or, using array extras
Object.entries[obj].forEach[[[key, value]] => {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}];
7 thành const obj = { foo: 'bar', baz: 42 };
console.log[Object.entries[obj]]; // [ ['foo', 'bar'], ['baz', 42] ]
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.entries[obj]]; // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.entries[anObj]]; // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
// getFoo is property which isn't enumerable
const myObj = Object.create[{}, { getFoo: { value[] { return this.foo; } } }];
myObj.foo = 'bar';
console.log[Object.entries[myObj]]; // [ ['foo', 'bar'] ]
// non-object argument will be coerced to an object
console.log[Object.entries['foo']]; // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
// returns an empty array for any primitive type except for strings [see the above example], since primitives have no own properties
console.log[Object.entries[100]]; // [ ]
// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for [const [key, value] of Object.entries[obj]] {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}
// Or, using array extras
Object.entries[obj].forEach[[[key, value]] => {
console.log[`${key} ${value}`]; // "a 5", "b 7", "c 9"
}];
8:const obj = { foo: 'bar', baz: 42 };
const map = new Map[Object.entries[obj]];
console.log[map]; // Map[2] {"foo" => "bar", "baz" => 42}
Lặp qua một đối tượng
Sử dụng phá hủy mảng, bạn có thể lặp lại thông qua các đối tượng một cách dễ dàng.
const obj = { foo: 'bar', baz: 42 };
Object.entries[obj].forEach[[[key, value]] => console.log[`${key}: ${value}`]]; // "foo: bar", "baz: 42"
Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # sec-object.entries # sec-object.entries |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt