1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
'use strict'; const p = console.log; /** callback(value, key, obj) */ const each = (obj, callback) => { const ret = []; for (const key of Object.keys(obj)) { const value = obj[key]; ret.push(callback(value, key, obj)); } return ret; }; const obj = { 名前: 'taro', 苗字: '田中', age: 27, }; const ret = each(obj, (v, k) => `${k}: ${v}`); ret.forEach((v) => p(v)); |
1 2 3 |
名前: taro 苗字: 田中 age: 27 |