2016-07-05 34 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