jQuery: Looping through object properly?

前端 未结 2 1258
悲哀的现实
悲哀的现实 2021-02-12 16:43

I am trying to loop through the below shown JS object with the following code snippet, while needing to fetch both the index key as well as the inner object.

How on ear

2条回答
  •  梦如初夏
    2021-02-12 17:24

    You could use a for in loop:

        var myObject = ({ prop_1:["1", "2"], prop_2:["3", "4"]})
        for (var key in myObject) {
           if (myObject.hasOwnProperty(key)) {
               alert(key + "/" + myObject[key]);
            }
         }
    

提交回复
热议问题