Why is “forEach not a function” for this object?

后端 未结 3 1612
梦毁少年i
梦毁少年i 2020-12-13 05:58

This is probably something really dumb, but I don\'t understand why this doesn\'t work.

var a = {\"cat\":\"large\"};

a.forEach(function(value, key, map){
           


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 06:43

    If you really need to use a secure foreach interface to iterate an object and make it reusable and clean with a npm module, then use this, https://www.npmjs.com/package/foreach-object

    Ex:

    import each from 'foreach-object';
       
    const object = {
       firstName: 'Arosha',
       lastName: 'Sum',
       country: 'Australia'
    };
       
    each(object, (value, key, object) => {
       console.log(key + ': ' + value);
    });
       
    // Console log output will be:
    //      firstName: Arosha
    //      lastName: Sum
    //      country: Australia
    

提交回复
热议问题