Loop through properties in JavaScript object with Lodash

后端 未结 7 1939
闹比i
闹比i 2021-02-01 00:01

Is it possible to loop through the properties in a JavaScript object? For instance, I have a JavaScript object defined as this:

myObject.options = {
  property1:         


        
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 00:39

    Yes you can and lodash is not needed... i.e.

    for (var key in myObject.options) {
      // check also if property is not inherited from prototype
      if (myObject.options.hasOwnProperty(key)) { 
        var value = myObject.options[key];
      }
    }
    

    Edit: the accepted answer (_.forOwn()) should be https://stackoverflow.com/a/21311045/528262

提交回复
热议问题