基于子对象的特定键值以排序顺序迭代JavaScript对象

时间:2010-10-28 10:06:07

标签: javascript json sorting hashtable iteration

简短版本:我正在寻找与Perl的

相当的JavaScript
for my $key ( sort { $hash{$a}{foo} cmp $hash{$b}{foo} } keys %hash ) {
    # do something with $key
}

更多细节:

我有一个JSON对象,它由一堆其他JSON对象组成,这些对象具有相同的属性,就像Perl中的哈希哈希:例如:

var peopleobj = { 
    "0291" : { "Forename" : "Jeremy", "Surname" : "Dyson" },
    "0398" : { "Forename" : "Billy", "Surname" : "Bunter" },
    "6714" : { "Forename" : "Harry", "Surname" : "Peterson" },
    "9080" : { "Forename" : "Barry", "Surname" : "Mainwaring"}
}

我想按照姓氏值的顺序遍历peopleobj中的对象,例如以姓氏顺序打印出姓名。简单的JavaScript或jQuery解决方案将在部署它的上下文中工作。

提前感谢您宝贵的时间。

1 个答案:

答案 0 :(得分:9)

有趣的问题......一个简单的JavaScript解决方案是基于'Surname'属性在单独的数组中为对象创建索引。像这样 1

var peopleobj = { 
   "0291" : { "Forename" : "Jeremy", "Surname" : "Dyson" },
   "0398" : { "Forename" : "Billy", "Surname" : "Bunter" },
   "6714" : { "Forename" : "Harry", "Surname" : "Peterson" },
   "9080" : { "Forename" : "Barry", "Surname" : "Mainwaring" }
};

var index = [];

// build the index
for (var x in peopleobj) {
   index.push({ 'key': x, 'Surname': peopleobj[x]['Surname'] });
}

// sort the index
index.sort(function (a, b) { 
   var as = a['Surname'], 
       bs = b['Surname']; 

   return as == bs ? 0 : (as > bs ? 1 : -1); 
}); 

现在您可以遍历index数组:

for (var i = 0; i < index.length; i++) {
   console.log(peopleobj[index[i]['key']]['Surname']);
}

结果(在Firebug控制台中测试):

Bunter
Dyson
Mainwaring
Peterson

你可能希望将它包装成某种可重用的Iterator对象,即使它很难像Perl一样简洁:

// Our reusable Iterator class:
function MyIterator (o, key) {
   this.index = [];
   this.i = 0;
   this.o = o;

   for (var x in o) {
      this.index.push({ 'key': x, 'order': o[x][key] });
   }

   this.index.sort(function (a, b) { 
      var as = a['order'], 
          bs = b['order']; 

      return as == bs ? 0 : (as > bs ? 1 : -1); 
   }); 

   this.len = this.index.length;
}

MyIterator.prototype.next = function () {
   return this.i < this.len ?
          this.o[this.index[this.i++]['key']] :
          null;
};

然后按如下方式使用:

// Our JavaScript object:
var peopleobj = { 
   "0291" : { "Forename" : "Jeremy", "Surname" : "Dyson" },
   "0398" : { "Forename" : "Billy", "Surname" : "Bunter" },
   "6714" : { "Forename" : "Harry", "Surname" : "Peterson" },
   "9080" : { "Forename" : "Barry", "Surname" : "Mainwaring" }
};

// Build the Iterator object, using the 'Surname' field:
var surnameIter = new MyIterator(peopleobj, 'Surname');

// Iterate:
var i;

while (i = surnameIter.next()) {
   console.log(i['Surname'] + ' ' + i['Forename']);
}

结果:

Bunter Billy
Dyson Jeremy
Mainwaring Barry
Peterson Harry

1 您可能希望使用hasOwnProperty()方法确保属性属于您的对象,而不是从Object.prototype继承:

for (var x in peopleobj) {
   if (peopleobj.hasOwnProperty(x)) {
      index.push({ 'key': x, 'Surname': peopleobj[x]['Surname'] });
   }
}