gpt4 book ai didi

javascript - 如何将对象重组为对象数组?

转载 作者:行者123 更新时间:2023-11-30 17:30:24 25 4
gpt4 key购买 nike

我有这个对象

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"
}
];

最佳答案

应该这样做:

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_"]
});

关于javascript - 如何将对象重组为对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23197718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com