Loop through properties in JavaScript object with Lodash

后端 未结 7 1943
闹比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:25

    Lets take below object as example

    let obj = { property1: 'value 1', property2: 'value 2'};
    

    First fetch all the key in the obj

    let keys = Object.keys(obj) //it will return array of keys
    

    and then loop through it

    keys.forEach(key => //your way)
    

    just putting all together

    Object.keys(obj).forEach(key=>{/*code here*/})
    

提交回复
热议问题