我正在将 JavaScript 对象转换为数组,但结果我得到了一个null
这是我的代码
<div id="a"></div>
Object.prototype.toArray = function(){
var arr = [];
for (var i in this) {
arr.push(this[i]);
}
return arr;
}
var providers = {
facebooklike: "Facebook Like",
facebookrecommend : "Facebook Recommend",
facebooksend : "Facebook Send",
twittertweet : "Twitter Tweet",
linkedinshare : "LinkedIn Share",
linkedinrecommend : "LinkedIn Recommend",
googleplusplusone : "Google+ +1",
googleplusshare : "Google+ Share"
};
var a = document.getElementById("a");
a.innerHTML = JSON.stringify(providers.toArray());
我的结果是
["Facebook Like","Facebook Recommend","Facebook Send","Twitter Tweet","LinkedIn Share","LinkedIn Recommend","Google+ +1","Google+ Share",null]
这是小提琴示例