2016-07-05 30 views
-1

这里一个多维数组或对象是我的代码:(我试图动态地构建angularJs的关联数组)如何动态地构建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"] 

请给我一个解决方案。谢谢。

+1

只是将'$ scope.socialProfiles [domain]'初始化为一个空数组,并将它推入它而不是将其设置为url – rob

+0

感谢rob,它的工作原理。 – Vimal

回答

0

您正在为每个新网站重新分配$ scope.socialProfiles [domain],而不是追加到它。的

代替

$scope.socialProfiles[domain]=url; 

尝试

if(!$scope.socialProfiles[domain]) 
    $scope.socialProfiles[domain] = [] 

$scope.socialProfiles[domain].push(url); 
+0

谢谢扎克,它看起来简单而完美。 – Vimal

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); 
}); 
+0

谢谢Aximus,它的工作原理。 – Vimal

+0

而不是使用 ** $ scope.socialProfiles [domain] = $ scope.socialProfiles [domain] || []; **, 使用 ** if(!$ scope.socialProfiles [domain]) $ scope.socialProfiles [domain] = [] ** 可以避免不必要的重新分配$ scope.socialProfiles [域]。 – Vimal

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是工作提琴

+0

谢谢贾斯汀赫特,伟大的解决方案。 – Vimal