这是我的代码:(我试图在angularJs中动态构造一个关联数组)
$scope.details = {"profilesFound": [
"https: //twitter.com/alexandreagular",
"https: //twitter.com/?profile_id=46130006",
"https: //facebook.com/1290628666",
"https: //twitter.com/intent/user?user_id=84326171",
"https: //www.linkedin.com/in/alex",
"https: //www.linkedin.com/in/alexandreagular",
"https: //www.facebook.com/alexandre.agular",
"https: //www.facebook.com/alexandre"
]};
$scope.socialProfiles = [];
var spLength = $scope.details.profilesFound.length;
for(var i=0;i<spLength;i++){
var url = $scope.details.profilesFound[i];
var domain = url.replace('.com','').replace('http://www.','').replace('https://www.','').replace('http://','').replace('https://','').split(/[/?#]/)[0];
$scope.socialProfiles[domain]=url;
}
我想构建一个数组,如下所示
"profilesFound":{
"facebook":[
"https://facebook.com/1290628666",
"https://www.facebook.com/alexandre.agular"
],
"twitter":[
"https: //twitter.com/alexandreagular",
"https: //twitter.com/?profile_id=46130006",
"https: //twitter.com/intent/user?user_id=84326171"
],
"linkedin":[
"https: //linkedin.com/1290628666",
"https: //www.linkedin.com/alexandre.agular"
]
}
但这就是我得到的结果。
[twitter: "https://twitter.com/intent/user?user_id=84326171", facebook: "https://www.facebook.com/alexandre.agular", linkedin: "https://www.linkedin.com/in/alexandreagular"]
请给我一个解决方案。感谢。
答案 0 :(得分:0)
您为每个新网站重新分配$ scope.socialProfiles [域名],而不是附加到该网站。
而不是
$scope.socialProfiles[domain]=url;
尝试
if(!$scope.socialProfiles[domain])
$scope.socialProfiles[domain] = []
$scope.socialProfiles[domain].push(url);
答案 1 :(得分:0)
$scope.details = {
profilesFound: [
"https://twitter.com/alexandreagular",
"https://twitter.com/?profile_id=46130006",
"https://facebook.com/1290628666",
"https://twitter.com/intent/user?user_id=84326171",
"https://www.linkedin.com/in/alex",
"https://www.linkedin.com/in/alexandreagular",
"https://www.facebook.com/alexandre.agular",
"https://www.facebook.com/alexandre"
]
},
$scope.socialProfiles = {};
$scope.details.profilesFound.forEach(function(profile) {
var domain = profile.replace('.com', '').replace('http://www.', '').replace('https://www.', '').replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
$scope.socialProfiles[domain] = $scope.socialProfiles[domain] || [];
$scope.socialProfiles[domain].push(profile);
});
答案 2 :(得分:0)
试试这个:
$scope.details = {"profilesFound": [
"https://twitter.com/alexandreagular",
"https://twitter.com/?profile_id=46130006",
"https://facebook.com/1290628666",
"https://twitter.com/intent/user?user_id=84326171",
"https://www.linkedin.com/in/alex",
"https://www.linkedin.com/in/alexandreagular",
"https://www.facebook.com/alexandre.agular",
"https://www.facebook.com/alexandre"
]};
$scope.object = {};
$scope.details.profilesFound.forEach(function (item, index) {
var base = item.split('.com');
var root = base[0].replace('http://www.', '').replace('https://www.','').replace('http://', '').replace('https://','');
if(typeof $scope.object[root] !== 'undefined') $scope.object[root].push(item);
else $scope.object[root] = [item];
console.log($scope.object);
});
Here是一个工作小提琴