作者:廖盈秀怡伶 | 来源:互联网 | 2023-10-10 21:48
给定以下JSON对象,是否有一种简单的方法来只提取结果对象属性的值?
var j={"success":true,
"msg":["Clutch successfully updated."],
"results":{"count_id":2, "count_type":"Clutch", "count_date":"2000-01-01", "fish_count":250, "count_notes":"test"}
};
var arr= doSomething(j.results);
//arr=[2, "Clutch","2000-01-01",250,"test"]
解决方法:
你的功能就像是
var doSomething = function (obj) {
var arr = [];
for (var x in obj) if (obj.hasOwnProperty(x)) {
arr.push(obj[x]);
}
return arr;
}