我可以以相反的顺序遍历javascript对象吗?

时间:2022-10-30 10:28:27

So I have a JavaScript object like this:

所以我有一个像这样的JavaScript对象:

foo = {
  "one": "some",
  "two": "thing",
  "three": "else"
};

I can loop this like:

我可以像这样循环:

for (var i in foo) {
  if (foo.hasOwnProperty(i)) {
    // do something
  }
}

Which will loop through the properties in the order of one > two > three.

这将以一个>两个>三个的顺序循环遍历属性。

However sometimes I need to go through in reverse order, so I would like to do the same loop, but three > two > one.

但是有时我需要以相反的顺序进行,所以我想做同样的循环,但是三个>两个>一个。

Question:
Is there an "object-reverse" function. If it was an Array, I could reverse or build a new array with unshift but I'm lost with what to do with an object, when I need to reverse-loop it's properties. Any ideas?

问题:是否存在“对象反转”功能。如果它是一个数组,我可以反转或构建一个新的数组,但是当我需要反向循环它的属性时,我会失去对象的操作。有任何想法吗?

Thanks!

2 个解决方案

#1


35  

Javascript objects don't have a guaranteed inherent order, so there doesn't exist a "reverse" order.

Javascript对象没有保证的固有顺序,因此不存在“反向”顺序。

4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

4.3.3对象对象是Object类型的成员。它是一个无序的属性集合,每个属性都包含一个原始值,对象或函数。存储在对象属性中的函数称为方法。

Browsers do seem to return the properties in the same order they were added to the object, but since this is not standard, you probably shouldn't rely on this behavior.

浏览器似乎确实以与添加到对象相同的顺序返回属性,但由于这不是标准,因此您可能不应该依赖此行为。

A simple function that calls a function for each property in reverse order as that given by the browser's for..in, is this:

一个简单的函数,它以与浏览器for..in给出的顺序相反的顺序调用每个属性的函数,如下所示:

// f is a function that has the obj as 'this' and the property name as first parameter
function reverseForIn(obj, f) {
  var arr = [];
  for (var key in obj) {
    // add hasOwnPropertyCheck if needed
    arr.push(key);
  }
  for (var i=arr.length-1; i>=0; i--) {
    f.call(obj, arr[i]);
  }
}

//usage
reverseForIn(obj, function(key){ console.log('KEY:', key, 'VALUE:', this[key]); });

Working JsBin: http://jsbin.com/aPoBAbE/1/edit

工作JsBin:http://jsbin.com/aPoBAbE/1/edit

Again i say that the order of for..in is not guaranteed, so the reverse order is not guaranteed. Use with caution!

我再次说for..in的顺序不保证,所以不保证相反的顺序。谨慎使用!

#2


5  

There is no way to loop through an object backwards, but if you recreate the object in reverse order then you are golden! Be cautions however, there is nothing that says the order of the object will stay the same as it changes and so this may lead to some interesting outcome, but for the most part it works...

没有办法向后循环一个对象,但如果你以相反的顺序重新创建对象,那么你就是金色!但是要注意,没有任何东西可以说对象的顺序会保持不变,所以这可能会带来一些有趣的结果,但是大多数情况下它都有效......

function ReverseObject(Obj){
    var TempArr = [];
    var NewObj = [];
    for (var Key in Obj){
        TempArr.push(Key);
    }
    for (var i = TempArr.length-1; i >= 0; i--){
        NewObj[TempArr[i]] = [];
    }
    return NewObj;
}

The just do the swap on your object like this-

只需像你这样交换你的对象 -

MyObject = ReverseObject(MyObject);

The loop would then look like this-

然后循环看起来像这样 -

for (var KeysAreNowBackwards in MyObject){
    alert(MyObject[KeysAreNowBackwards]);
} 

#1


35  

Javascript objects don't have a guaranteed inherent order, so there doesn't exist a "reverse" order.

Javascript对象没有保证的固有顺序,因此不存在“反向”顺序。

4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

4.3.3对象对象是Object类型的成员。它是一个无序的属性集合,每个属性都包含一个原始值,对象或函数。存储在对象属性中的函数称为方法。

Browsers do seem to return the properties in the same order they were added to the object, but since this is not standard, you probably shouldn't rely on this behavior.

浏览器似乎确实以与添加到对象相同的顺序返回属性,但由于这不是标准,因此您可能不应该依赖此行为。

A simple function that calls a function for each property in reverse order as that given by the browser's for..in, is this:

一个简单的函数,它以与浏览器for..in给出的顺序相反的顺序调用每个属性的函数,如下所示:

// f is a function that has the obj as 'this' and the property name as first parameter
function reverseForIn(obj, f) {
  var arr = [];
  for (var key in obj) {
    // add hasOwnPropertyCheck if needed
    arr.push(key);
  }
  for (var i=arr.length-1; i>=0; i--) {
    f.call(obj, arr[i]);
  }
}

//usage
reverseForIn(obj, function(key){ console.log('KEY:', key, 'VALUE:', this[key]); });

Working JsBin: http://jsbin.com/aPoBAbE/1/edit

工作JsBin:http://jsbin.com/aPoBAbE/1/edit

Again i say that the order of for..in is not guaranteed, so the reverse order is not guaranteed. Use with caution!

我再次说for..in的顺序不保证,所以不保证相反的顺序。谨慎使用!

#2


5  

There is no way to loop through an object backwards, but if you recreate the object in reverse order then you are golden! Be cautions however, there is nothing that says the order of the object will stay the same as it changes and so this may lead to some interesting outcome, but for the most part it works...

没有办法向后循环一个对象,但如果你以相反的顺序重新创建对象,那么你就是金色!但是要注意,没有任何东西可以说对象的顺序会保持不变,所以这可能会带来一些有趣的结果,但是大多数情况下它都有效......

function ReverseObject(Obj){
    var TempArr = [];
    var NewObj = [];
    for (var Key in Obj){
        TempArr.push(Key);
    }
    for (var i = TempArr.length-1; i >= 0; i--){
        NewObj[TempArr[i]] = [];
    }
    return NewObj;
}

The just do the swap on your object like this-

只需像你这样交换你的对象 -

MyObject = ReverseObject(MyObject);

The loop would then look like this-

然后循环看起来像这样 -

for (var KeysAreNowBackwards in MyObject){
    alert(MyObject[KeysAreNowBackwards]);
}