Tengo lo siguiente que itera a través de un objeto/matriz para imprimir sus propiedades enumerables:
const reg = /(?<num>hi)(there)/g; const str = 'hithere'; let matches = Array.from(str.matchAll(reg)); // same thing as [...matches[;]] for (let match of matches) { for (let elem of match) { console.log('**', elem); } }El objeto real se ve así:
[ 'hithere'. // enumerable 'hi', // enumerable 'there', // enumerable index: 0, // no input: 'hithere', // no groups: [Object: null prototype] { num: 'hi' } // no ], ... ¿Existe una forma más directa de (1) obtener las propiedades enumerables en un objeto; o (2) prueba para ver si una propiedad de objeto es enumerable? Pensé que lo siguiente funcionaría, pero parece que siempre se imprime como true para mí:
matches[0].propertyIsEnumerable('index'));Esa es una muy buena pregunta.
for... of no recorre necesariamente todas las propiedades enumerables de un objeto de la forma en que lo hace for... in. Los objetos Javascript pueden definir su propio protocolo iterable .
Los valores booleanos que obtiene de propertyIsEnumerable son correctos.
Entonces, para responder a sus preguntas directamente: (1) sí: a for... in loop (2) no: su prueba funciona bien
const reg = /(?<num>hi)(there)/g; const str = 'hithere'; let matches = Array.from(str.matchAll(reg)); // same thing as [...matches[;]] for (let match in matches) { for (let elem in matches[match]) { console.log(elem, matches[match][elem]); } }