jQuery access JSON Object

后端 未结 5 1788
梦毁少年i
梦毁少年i 2021-02-06 04:14

How do I access the name of an item in an Object Literal using jQuery?

For example how would I read \"title\", \"link\", \"media\", ect... in this

{
             


        
5条回答
  •  遥遥无期
    2021-02-06 04:48

    You can also use the $.each function:

    var obj = { one:1, two:2, three:3, four:4, five:5 };
    
    $.each(obj, function(key, value) {
      //..
    });
    

    If you go for the for...in statement way, I would recommend you to check if the property resides directly on the object being iterated, because you could have some issues, if the Object.prototype is extended:

    for(var key in obj) {
      if (obj.hasOwnProperty(key)){
        // value = obj[key];
      }
    }
    

提交回复
热议问题