<i id='hSJks'><tr id='hSJks'><dt id='hSJks'><q id='hSJks'><span id='hSJks'><b id='hSJks'><form id='hSJks'><ins id='hSJks'></ins><ul id='hSJks'></ul><sub id='hSJks'></sub></form><legend id='hSJks'></legend><bdo id='hSJks'><pre id='hSJks'><center id='hSJks'></center></pre></bdo></b><th id='hSJks'></th></span></q></dt></tr></i><div id='hSJks'><tfoot id='hSJks'></tfoot><dl id='hSJks'><fieldset id='hSJks'></fieldset></dl></div>
  • <legend id='hSJks'><style id='hSJks'><dir id='hSJks'><q id='hSJks'></q></dir></style></legend>
    1. <small id='hSJks'></small><noframes id='hSJks'>

        <bdo id='hSJks'></bdo><ul id='hSJks'></ul>

        <tfoot id='hSJks'></tfoot>

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

        Iterating over a JavaScript object in sort order based on particular key value of a child object(基于子对象的特定键值以排序顺序迭代 JavaScript 对象)

        <tfoot id='rE7UB'></tfoot>

                <tbody id='rE7UB'></tbody>
                <bdo id='rE7UB'></bdo><ul id='rE7UB'></ul>

                <legend id='rE7UB'><style id='rE7UB'><dir id='rE7UB'><q id='rE7UB'></q></dir></style></legend>

                <i id='rE7UB'><tr id='rE7UB'><dt id='rE7UB'><q id='rE7UB'><span id='rE7UB'><b id='rE7UB'><form id='rE7UB'><ins id='rE7UB'></ins><ul id='rE7UB'></ul><sub id='rE7UB'></sub></form><legend id='rE7UB'></legend><bdo id='rE7UB'><pre id='rE7UB'><center id='rE7UB'></center></pre></bdo></b><th id='rE7UB'></th></span></q></dt></tr></i><div id='rE7UB'><tfoot id='rE7UB'></tfoot><dl id='rE7UB'><fieldset id='rE7UB'></fieldset></dl></div>
                • <small id='rE7UB'></small><noframes id='rE7UB'>

                  本文介绍了基于子对象的特定键值以排序顺序迭代 JavaScript 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  短版:我正在寻找 Perl 的 JavaScript 等价物

                  Short version: I'm looking for the JavaScript equivalent of Perl's

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

                  更多细节:

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

                  I have a JSON object which consists of a bunch of other JSON objects which have identical properties to each other, like a hash of hashes in Perl: eg:

                  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 解决方案将在部署它的上下文中工作.

                  I want to iterate through the objects in peopleobj in order of the surname values, eg to print out the names in surname order. Plain JavaScript or jQuery solutions will work in the context in which this is being deployed.

                  提前感谢您的宝贵时间.

                  Thanks in advance for your valuable time.

                  推荐答案

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

                  Interesting question... One plain JavaScript solution is to create an index for your objects in a separate array, based on the 'Surname' property. Something like this1:

                  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 数组了:

                  Now you would be able to iterate over your index array:

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

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

                  Result (Tested in Firebug console):

                  Bunter
                  Dyson
                  Mainwaring
                  Peterson
                  

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

                  You may want to wrap this up into some sort of reusable Iterator object, even though it would be difficult to get as terse as 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'] });
                     }
                  }
                  

                  这篇关于基于子对象的特定键值以排序顺序迭代 JavaScript 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                    <tbody id='XUZZ8'></tbody>

                    <legend id='XUZZ8'><style id='XUZZ8'><dir id='XUZZ8'><q id='XUZZ8'></q></dir></style></legend>
                  1. <small id='XUZZ8'></small><noframes id='XUZZ8'>

                    <i id='XUZZ8'><tr id='XUZZ8'><dt id='XUZZ8'><q id='XUZZ8'><span id='XUZZ8'><b id='XUZZ8'><form id='XUZZ8'><ins id='XUZZ8'></ins><ul id='XUZZ8'></ul><sub id='XUZZ8'></sub></form><legend id='XUZZ8'></legend><bdo id='XUZZ8'><pre id='XUZZ8'><center id='XUZZ8'></center></pre></bdo></b><th id='XUZZ8'></th></span></q></dt></tr></i><div id='XUZZ8'><tfoot id='XUZZ8'></tfoot><dl id='XUZZ8'><fieldset id='XUZZ8'></fieldset></dl></div>

                        <tfoot id='XUZZ8'></tfoot>

                        • <bdo id='XUZZ8'></bdo><ul id='XUZZ8'></ul>