这里一个多维数组或对象是我的代码:(我试图动态地构建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"]
请给我一个解决方案。谢谢。
只是将'$ scope.socialProfiles [domain]'初始化为一个空数组,并将它推入它而不是将其设置为url – rob
感谢rob,它的工作原理。 – Vimal