2015-12-07 79 views
0

我有,我想根據年齡值重建一個嵌套的數組:重建嵌套數組

//Data 
data = {"people": 
    [{"male_1": [ 
    {"name": "Bob" ,"age": "32"}, 
    {"name":"Mike", "age":"31"} 
    ]}, 
    {"female_2": [ 
    {"name":"Jessica", "age": "24"}, 
    {"name":"Ann", "age": "23"} 
    ]} 
    [{"male_3": [ 
    {"name": "Tom" ,"age": "31"}, 
    {"name":"John", "age":"29"} 
    ]}, ... 
    ]} 

新數組應該是這樣的:

people = [{"male_1": [ 
     {"name": "Bob" ,"age": "32"} 
    ]}, 
    [{"male_3": [ 
     {"name": "Tom" ,"age": "31"} 
    ]}, 
    {"female_2": [ 
    {"name":"Jessica", "age": "24"} 
    ]}, ... 
    ]} 

基於這個例子,我需要找到所有「人」的最大年齡,然後將此人添加到數組,然後與下一個相同。在這種情況下,年齡可以相同,誰先排名沒有區別。

隨着下一個功能,我可以找到第一個,並推到新陣列,但如何找到下一個?

var age = 0; 

data["people"].forEach(function(item) { 
    for (var key in item) { 
     if (item.hasOwnProperty(key)) { 
      item[key].forEach(function(person) { 
       if (person.age > age) { 
        age = person.age; 
        oldest_person = person 

       } 

      }); 
     } 
    } 
}); 
console.log(oldest_person); 
+0

你有沒有考慮使用Angulars [排序依據](https://docs.angularjs.org/api/ng/filter/orderBy)? –

+0

你的意思是自定義過濾器?我需要在控制器內重建它,而不是在'ng-repeat'中 –

回答

0

試試這個:

var age = 0; 
var oldest_person = []; 
var data = {"people": 
    [ 
    {"male_1": [ 
    {"name": "Bob" ,"age": "32"}, 
    {"name":"Mike", "age":"31"} 
    ]}, 
    {"female_2": [ 
    {"name":"Jessica", "age": "24"}, 
    {"name":"Ann", "age": "23"} 
    ]}, 
    {"male_3": [ 
    {"name": "Tom" ,"age": "31"}, 
    {"name":"John", "age":"29"} 
    ]} 
]}; 

data["people"].forEach(function(item) { 
    for (var key in item) { 
     if (item.hasOwnProperty(key)) { 
      var age = 0; 
      var name = key; 
      var oldest = null; 
      item[key].forEach(function(person) { 
       // Determine the oldest person in each group ("male_1", "female_2", "male_3", ...) 
       if (person.age > age) { 
        age = person.age; 
        oldest = person; 
       } 
      }); 
      // Push the oldest person into the 'oldest_person' array 
      var group = {}; 
      group[name] = [ oldest ]; 
      oldest_person.push(group); 
     } 
    } 
}); 

console.log(oldest_person); 
0

你可以使用一些Array方法,如Array.prototype.forEach()Array.prototype.reduce()Object.keys()

var data = { "people": [{ "male_1": [{ "name": "Bob", "age": "32" }, { "name": "Mike", "age": "31" }] }, { "female_2": [{ "name": "Jessica", "age": "24" }, { "name": "Ann", "age": "23" }] }, { "male_3": [{ "name": "Tom", "age": "31" }, { "name": "John", "age": "29" }] }] }, 
 
    people = []; 
 
data.people.forEach(function (a) { 
 
    Object.keys(a).forEach(function (k) { 
 
     var o = {}; 
 
     o[k] = a[k].reduce(function (c, d) { 
 
      return c.age > d.age ? c : d; 
 
     }); 
 
     people.push(o); 
 
    }); 
 
}); 
 
document.write('<pre>' + JSON.stringify(people, 0, 4) + '</pre>');

1

這裏是另一種解釋。這一個使用本地Array.prototype.sort作爲幫助功能。

var data = { "people": [{ "male_1": [{ "name": "Bob", "age": "32" }, { "name": "Mike", "age": "31" }] }, { "female_2": [{ "name": "Jessica", "age": "24" }, { "name": "Ann", "age": "23" }] }, { "male_3": [{ "name": "Tom", "age": "31" }, { "name": "John", "age": "29" }] }] }, 
 
\t oldies = [], 
 
\t peopleByAge = data.people.map(function(group){ 
 

 
\t for(var name in group){ 
 
\t \t group[name] = group[name].sort(sortBy('age')); 
 
\t \t oldies.push(group[name][0]); 
 
\t } 
 

 
\t return group; 
 
}); 
 
// sort by an object key 
 
function sortBy(key){ 
 
\t return function(a, b){ 
 
\t \t return parseInt(a[ key ]) < parseInt(b[ key ]); 
 
\t } 
 
} 
 

 
document.write('<pre>' + JSON.stringify({ oldies: oldies.sort(sortBy('age')), peopleByAge: peopleByAge }, 0, 2) + '</pre>');