2015-12-02 73 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);