2015-12-02 76 views
0

我稱爲「國家」,其看起來像一個陣列:組或重新安排基於條件數組元素 - 的JavaScript

country=[ 
      { 
       "name": "china", 
       "id": "country:china" 
      }, { 
       "name": "city1", 
       "id": "country:country1>city1" 
      }, { 
       "name": "city2", 
       "id": "country:country1>city2" 
      }, { 
       "name": "city3", 
       "id": "country:country1>city3" 
      }, { 
       "name": "korea", 
       "id": "country:korea" 
      }, { 
       "name": "australia", 
       "id": "country:australia" 
      } 
     ] 

我期待在重排/分組上述陣列:

countryResult = [ china, country1(city1, city2, city3), korea, australia] 

我寫了下面的代碼,但是這並沒有給我想要的結果:

$scope.countryInfo = function(itemData){ 
     var countryResult = []; 
     for(var i=0; i<itemData.length; i++){ 
      var countryItem = itemData[i]; 
      if(countryItem.id.indexOf('>') > -1){ //Found city 
       var itemId = countryItem.id.substr(0, countryItem.id.indexOf('>')); 
       for(var j=0; j<$scope.countryData.length; j++){ 
        if($scope.countryData[j].id == itemId){ 
         var _parentChild = $scope.countryData[j].name + "(" + countryItem.name + ") "; 
         countryResult.push(_parentChild); 
        } 
       } 
      } 
      else{ 
       countryResult.push(countryItem.name); 
      } 
     } 
     return countryResult; 
    } 

結果是合作像這樣 - [ china, country1(city1), country1(city2), country1(city3)), korea, australia]

請讓我知道如何實現預期的數組結果。

編輯:我只是在尋找簡化陣列[ china, country1(city1), country1(city2), country1(city3)), korea, australia][ china, country1(city1, city2, city3), korea, australia]

回答

0

您可以使用一個臨時的對象,然後將對象的屬性映射到想要的陣列。

var country = [{ "name": "china", "id": "country:china" }, { "name": "city1", "id": "country:country1>city1" }, { "name": "city2", "id": "country:country1>city2" }, { "name": "city3", "id": "country:country1>city3" }, { "name": "korea", "id": "country:korea" }, { "name": "australia", "id": "country:australia" }], 
 
    temp = {}, 
 
    result; 
 

 
country.forEach(function (a) { 
 
    var b = a.id.split(/\:|\>/g); 
 
    temp[b[1]] = temp[b[1]] || []; 
 
    if (b[2]) { 
 
     temp[b[1]].push(b[2]); 
 
    } 
 
}); 
 
result = Object.keys(temp).map(function (k) { 
 
    if (temp[k].length) { 
 
     return k + '(' + temp[k].join(', ') + ')'; 
 
    } 
 
    return k; 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

+0

我試圖實施您的解決方案。在我可以加入元素之前,我需要用另一個文本替換臨時圖的關鍵字。在這樣做的過程中,我迷失了方向。請讓我知道如何將[china,country1(city1),country1(city2),country1(city3)),korea,australia [array1]澳大利亞] – Mustang

0

我用reduce與初始對象與keys屬性捕獲元素的位置:

function sorter(countty) { 
    var obj = country.reduce(function (p, c, i) { 
    var key, id = c.id.split(/[:>]/); 
    if (id.length === 3) { 
     key = id[1]; 
     if (!p[key]) { 
     p[key] = []; 
     p.keys.push(key); 
     } 
     p[key].push(id[2]); 
    } else { 
     p.keys.push(c.name); 
    } 
    return p; 
    }, { keys: [] }); 

    return obj.keys.map(function (el) { 
    return obj[el] ? el + '(' + obj[el].join(', ') + ')' : el; 
    }); 
} 

sorter(country); 

DEMO

0

我寫此位的代碼來改變你的第一個數組(在你的「編輯」)到你的第二個數組中,我不是說是乾淨的,但它的工作原理:

排序數組,然後試圖弄清楚,如果各國匹配,如果他們有需要合併的城市...

//an array of countries, some of which include comma delimited cities wrappedin parentheses 
 
var arrayPlaces = ["china", "country1(city1)", "korea", "country1", "country1(city2)", "country1(city3)", "australia", "korea", "home(delicious)", "home(nacho)", "home(plate)"]; 
 

 
//creates an alphabatized version of the array, to simplify finding matching countries 
 
var arrayPlacesSorted = arrayPlaces.sort(); 
 

 
//defines a regular expression (to search for matches countries) 
 

 

 
var hasCity = function (sTemp) { 
 
    var re = /\(/; 
 
    if (re.test(sTemp)) { 
 
    return true; 
 

 
    } else { 
 
    return false; 
 
    } 
 

 
}; 
 

 
var countryRe = /(.*?)\(.*?\)/; 
 
var cityRe = /.*?\((.*?)\)/; 
 

 
//function that loops through array, checks for matching countries, combines/adds cities 
 
var groupCities = function (aTemp) { 
 
    var currentCountry, 
 
    currentCity, 
 
    nextCountry, 
 
    nextCity; 
 

 
    for (var i = 0; i < (aTemp.length); i++) { 
 
    if (hasCity(aTemp[i])) { 
 
     currentCountry = countryRe.exec(aTemp[i])[1]; 
 
     currentCity = cityRe.exec(aTemp[i])[1]; 
 
    } else { 
 
     currentCountry = aTemp[i]; 
 
     currentCity = false; 
 
    } 
 
    if (hasCity(aTemp[i + 1])) { 
 
     nextCountry = countryRe.exec(aTemp[i + 1])[1]; 
 
     nextCity = cityRe.exec(aTemp[i + 1])[1]; 
 
    } else { 
 
     nextCountry = aTemp[i + 1]; 
 
     nextCity = false; 
 
    } 
 
    if (currentCountry === nextCountry && nextCity && currentCity) { 
 
     aTemp[i] = currentCountry + "(" + currentCity + "," + nextCity + ")"; 
 
     aTemp.splice(i + 1, 1); 
 
     i = 0; 
 
    } else if (currentCountry === nextCountry && nextCity) { 
 
     aTemp[i] = currentCountry + "(" + nextCity + ")"; 
 
     aTemp.splice(i + 1, 1); 
 
     i = 0; 
 
    } else if (currentCountry === nextCountry && currentCity) { 
 
     aTemp[i] = currentCountry + "(" + currentCity + ")"; 
 
     aTemp.splice(i + 1, 1); 
 
     i = 0; 
 
    } else if (currentCountry === nextCountry) { 
 
     aTemp[i] = currentCountry; 
 
     aTemp.splice(i + 1, 1); 
 
     i = 0; 
 
    } 
 
    } 
 
    return aTemp; 
 
}; 
 

 
var result = groupCities(arrayPlacesSorted); 
 
console.log(result);