如何隔离first_name键以在以下对象中输出其值?

{
    "first_name": "D",
    "last_name": "N",
    "phone_number": 1233414234
}

最佳答案

您可以使用.[]

 var x = {"first_name":"D","last_name":"N","phone_number": 1233414234};
 alert(x.first_name);
 alert(x['first_name']);


参考是here

编辑

根据另一个答案的注释中的OP问题:如果要遍历数组的对象,则可以使用每个对象。

var array = [{"first_name":"D","last_name":"N","phone_number": 1233414234},{"first_name":"P","last_name":"T","phone_number": 1233414234}];
$.each(array, function(idx) {
    alert(array[idx].first_name);
});


要么

var array = [{"first_name":"D","last_name":"N","phone_number": 1233414234},{"first_name":"P","last_name":"T","phone_number": 1233414234}];
$.each(array, function(idx, obj) {
    alert(obj.first_name);
});

关于javascript - 如何隔离“first_name”键以在以下对象中输出其值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38593021/

10-11 05:37