2016-09-06 87 views
-3

創建關聯多維數組對象我有一個數據庫表3列:如何在JavaScript

id | city  | area   | 
--------------------------------------   
2000 | Paris  | City center | 
2010 | Barcelona | Las Ramblas | 
2030 | Madrid  | City center | 
2400 | Paris  | City center | 
3600 | Barcelona | City center | 
3670 | Madrid  | City center | 
5000 | Paris  | Eiffel tower | 
7000 | Paris  | Eiffel tower | 
1000 | Barcelona | Las Ramblas | 
2500 | Madrid  | city center | 

值在id柱是獨一無二的!
據我試圖在屬於下城區得到每個區域的表:

$scope.cities = [ 
    { 
     name: "city_1", 
     areas: [ 
      {name: "area_1"}, 
      {name: "area_2"} 
     ] 
    }, 
    { 
     name: "city_2", 
     areas: [ 
      {name: "area_1"}, 
      {name: "area_2"}, 
      {name: "area_3"} 
     ] 
    }, 
    { 
     name: "city_3", 
     areas: [ 
      {name: "area_1"} 
     ] 
    } 
]; 

我已經要求添加JSON輸出,在這兒呢。

[{ 
    "id": "2000", 
    "city": "Paris", 
    "cityArea": "City center" 
}, { 
    "id": "2010", 
    "city": "Barcelona", 
    "cityArea": "Las Ramblas" 
}, { 
    "id": "2030", 
    "city": "Madrid", 
    "cityArea": "City center" 
}, { 
    "id": "2400", 
    "city": "Paris", 
    "cityArea": "City center" 
}, { 
    "id": "3600", 
    "city": "Barcelona", 
    "cityArea": "City center" 
}, { 
    "id": "3670", 
    "city": "Madrid", 
    "cityArea": "City center" 
}, { 
    "id": "5000", 
    "city": "Paris", 
    "cityArea": "Eiffel tower" 
}, { 
    "id": "7000", 
    "city": "Paris", 
    "cityArea": "Eiffel tower" 
}, { 
    "id": "1000", 
    "city": "Barcelona", 
    "cityArea": "Las Ramblas" 
}, { 
    "id": "2500", 
    "city": "Madrid", 
    "cityArea": "city center" 
}] 

我已經嘗試過:

var duplicatedCities = []; 
for(var i=0; i<$scope.results.length; i++) { 
    duplicatedCities.push($scope.results[i].city); 
} 

//output 
// ["Paris","Barcelona","Madrid","Paris","Barcelona","Madrid","Paris","Paris","Barcelona","Madrid"]; 

var uniqueCities = [];   
uniqueCities.push(duplicatedCities.filter(function(elem, index, self) { 
    return index == self.indexOf(elem); 
})); 

//output 
//["Paris","Barcelona","Madrid"]; 

,但現在什麼?

+0

[創建與多維數組的多維對象(http://stackoverflow.com/questions/11648586/create-multidimensional-object-with-multidimentional-arrays) – Tibrogargan

+0

聲音的可能的複製像一個挑戰。你有嘗試過什麼嗎?你如何從數據庫表中獲取數據到JavaScript? –

+0

你得到一個json字符串,並且想要按照描述創建'$ scope.cities'。對?然後提供該json。 –

回答

0

function GetNamedObj(arr, name) { 
 
    
 
     for (var i = 0; i < arr.length; i++) { 
 
      if (arr[i]["name"].toUpperCase() == name.toUpperCase()) { 
 
       return arr[i]; 
 
      } 
 
     } 
 
     // will return undefined if not found  
 
    } 
 
    
 
    function getCities(json) { 
 
     var data = JSON.parse(json); 
 
     var cities = []; 
 
    
 
     for (var i = 0; i < data.length; i++) { 
 
      var city = GetNamedObj(cities, data[i].city); 
 
      if (city) { 
 

 
       if (GetNamedObj(city.areas, data[i].cityArea)) continue; 
 
       var area = { 
 
        name: data[i].cityArea 
 
       }; 
 
       city.areas.push(area); 
 
      } 
 
      else { 
 
       var newCity = { 
 
        name: data[i].city, 
 
        areas: [{ 
 
         name: data[i].cityArea 
 
        }] 
 
       }; 
 
       cities.push(newCity); 
 
      } 
 
     } 
 
    
 
     return cities; 
 
    } 
 

 

 
    var cities = getCities("[{ \"id\": \"2000\", \"city\": \"Paris\", \"cityArea\": \"City center\"}, { \"id\": \"2010\", \"city\": \"Barcelona\", \"cityArea\": \"Las Ramblas\"}, { \"id\": \"2030\", \"city\": \"Madrid\", \"cityArea\": \"City center\"}, { \"id\": \"2400\", \"city\": \"Paris\", \"cityArea\": \"City center\"}, { \"id\": \"3600\", \"city\": \"Barcelona\", \"cityArea\": \"City center\"}, { \"id\": \"3670\", \"city\": \"Madrid\", \"cityArea\": \"City center\"}, { \"id\": \"5000\", \"city\": \"Paris\", \"cityArea\": \"Eiffel tower\"}, { \"id\": \"7000\", \"city\": \"Paris\", \"cityArea\": \"Eiffel tower\"}, { \"id\": \"1000\", \"city\": \"Barcelona\", \"cityArea\": \"Las Ramblas\"}, { \"id\": \"2500\", \"city\": \"Madrid\", \"cityArea\": \"city center\"}]"); 
 

 
console.log(cities);

+0

哇,謝謝!!只有一件事,爲什麼城市名稱仍然重複? – Paco

+0

城市不重複,但城市地區是。我沒有阻止它,因爲在這個問題上你已經允許重複的城市區域 –

+0

非常感謝你!我會檢查的。 – Paco

-1

最簡單的方式,利用城市作爲鍵:

foo = {}; 
foo['Paris'] = {}; 
foo['Paris'].areas = ['area1', 'area2']; 

如果你可能會得到重複的城市名稱,例如「新福堡」存在於多個國家,只需添加上述任何必要的州/國家/大陸/行星/星系/宇宙鍵來區分。

使用名稱作爲鍵可以非常容易地在對象/數組中找到某些東西 - 只需直接訪問它即可。如果您開始將名稱作爲值來掩埋,那麼您可以查看整個結構以找到它隱藏的位置。

+2

@hagay請記住,堆棧溢出不是一個代碼寫入服務。 –