-
[javascript] Objects.keys(array), Object.getOwnPropertyNames(array), Object.entries(array)컴퓨터/JavaScript_typescript 2020. 2. 6. 11:05
* get an array of its keys
var lunch = { sandwich: 'turkey', chips: 'cape cod', drink: 'soda' }; var keys = Object.keys(lunch);
returns enumerable properties
- var props = Object.getOwnPropertyNames(lunch)
returns both enumerable and non-enumerable properties
// returns an empty array: Object.keys(Function.prototype); // returns ["length", "name", "arguments", "caller", "constructor", "apply", "bind", "call", "toString"] Object.getOwnPropertyNames(Function.prototype);
cf) Array.forEach(), Array.map()
keys.forEach(function(key){console.log(key)});
* get an array of entries
var lunch = { sandwich: 'turkey', chips: 'cape cod', drink: 'soda' }; // returns [['sandwich', 'turkey'], ['chips', 'cape code'], ['drink', 'soda']] var entries = Object.entries(lunch);
* create an array from an object
var keys = ; var entries = ; for (var key in lunch) { if (lunch.hasOwnProperty(key)) { // Create an array of keys keys.push(key); // Create an array of entries entries.push([key, lunch[key]]); } }
source : [Go Make Things] Converting an object into an array with vanilla JS
'컴퓨터 > JavaScript_typescript' 카테고리의 다른 글
[JavaScript] Object.freeze : 객체를 불변하게 만들기, const(reference 규제)와의 차이점(freeze: 값을 규제) (0) 2020.02.11 [weird JavaScript] type coercion, ==와 > < operator, ++, --도 type coercion 사용 (0) 2020.02.11 JSON - parsing error (0) 2020.01.21 동적 타이핑, 타입 체크 (0) 2020.01.19 throw Error (0) 2020.01.19