How to set value to a property in a Javascript object, which is identified by an array of keys

前端 未结 2 1409
耶瑟儿~
耶瑟儿~ 2020-12-21 11:15

If there is a Javascript object with multiple levels, as in:

   myObject = {
         a: 12,
        obj11: {
                obj111: \'John\',
                      


        
2条回答
  •  萌比男神i
    2020-12-21 11:27

    function myFunc(myObj,myArr,newValue){
      var temp=myObj;
      for(var I=0;I

    Update: This is a classic example of how you can access properties from an object in JavaScript. You may, for simplicity, consider object as an array of properties. Now try and access the required property as if it is the index of the array. If u have a nested object, consider it as a nested array.

    Eg: console.log(myObj['obj11']['obj1111']);

    The above code will display { a:15, b: 35 }, before running the myFunc() and will display {z:12} after running it.

提交回复
热议问题