我有这个对象
var data = {
_id : 131233442543,
SocialMedia_0__Name_: "facebook",
SocialMedia_0__URL_: "http://facebook.com/test",
SocialMedia_1__Name_: "twitter",
SocialMedia_1__URL_: "http://twitter.com/test",
SocialMedia_1398083781749__Name_: "linkedin",
SocialMedia_1398083781749__URL_: "http://linkedin.com/test"
};
我需要重新构建对象:
justSites = [
{
name : "facebook",
url : "http://facebook.com/test"
},
{
name : "twitter",
url : "http://twitter.com/test"
},
{
name : "linkedin",
url : "http://linkedin.com/test"
}
];
答案 0 :(得分:2)
这应该这样做:
var justSites = [], m;
for (var p in data)
if (m = p.match(/SocialMedia_(\d+)__Name_/))
justSites.push({
name: data[p],
url: data["SocialMedia_"+m[1]+"__URL_"]
});