diff --git a/js/freq.ts b/js/freq.ts index bd90f49..cc34b51 100644 --- a/js/freq.ts +++ b/js/freq.ts @@ -17,3 +17,21 @@ Math.ceil(.95) // 1 // 给定数字的值四舍五入到最接近的整数。 Math.round(20.49); //20 Math.round(20.5); //21 + + +// For iterating on keys of Arrays, Strings, or Objects, use for .. in : +for (let key in yourobject) { + console.log(key, yourobject[key]); +} + +// With ES6, if you need both keys and values simultaneously, do +for (let [key, value] of Object.entries(yourobject)) { + console.log(key, value); +} + +// To avoid logging inherited properties, check with hasOwnProperty : +for (let key in yourobject) { + if (yourobject.hasOwnProperty(key)) { + console.log(key, yourobject[key]); + } +}