Javascript 如何在javascript中反转对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36958870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:45:55  来源:igfitidea点击:

How to reverse an object in javascript?

javascript

提问by Nilam

I want to reverse an object in JavaScript. For Example:

我想在 JavaScript 中反转一个对象。例如:

Input: obj ={1: 'banana', 2: 'apple',3:'orange' }

输入: obj ={1: 'banana', 2: 'apple',3:'orange' }

Output: ['orange','apple','banana' ]

输出: ['orange','apple','banana' ]

I have tried with 'Array.prototype.reverse.apply(obj)', but not able to get the result.

我曾尝试使用 'Array.prototype.reverse.apply(obj)',但无法获得结果。

 var obj ={1: 'banana', 2: 'apple',3:'orange' };
 var res =Array.prototype.reverse.apply(obj);
 console.log(res); // return the same object, not reverse

What are the other usages of Array.prototype.reverse()?

Array.prototype.reverse() 的其他用法是什么?

回答by Oriol

You can first convert your almost-array-like object to a real array, and then use .reverse():

您可以先将几乎类似于数组的对象转换为真正的数组,然后使用.reverse()

Object.assign([], {1:'banana', 2:'apple', 3:'orange'}).reverse();
// [ "orange", "apple", "banana", <1 empty slot> ]

The empty slot at the end if cause because your first index is 1instead of 0. You can remove the empty slot with .length--or .pop().

末尾的空槽是因为您的第一个索引1不是0. 您可以使用.length--或删除空插槽.pop()

Alternatively, if you want to borrow .reverseand call it on the same object, it must be a fully-array-like object. That is, it needs a lengthproperty:

或者,如果你想.reverse在同一个对象上借用和调用它,它必须是一个完全类似数组的对象。也就是说,它需要一个length属性:

Array.prototype.reverse.call({1:'banana', 2:'apple', 3:'orange', length:4});
// {0:"orange", 1:"apple", 3:"banana", length:4}

Note it will return the same fully-array-like object object, so it won't be a real array. You can then use deleteto remove the lengthproperty.

请注意,它将返回相同的完全类似数组的对象对象,因此它不会是真正的数组。然后您可以使用delete删除该length属性。

回答by Yossi Neiman

This is mine implementation of object rotation

这是我的对象旋转实现

    function reverseObject(object) {
        var newObject = {};
        var keys = [];

        for (var key in object) {
            keys.push(key);
        }

        for (var i = keys.length - 1; i >= 0; i--) {
          var value = object[keys[i]];
          newObject[keys[i]]= value;
        }       

        return newObject;
      }

回答by Konst

There is no point of doing this, cause object's properties do not have order. Properties order in objects is not guaranteed in JavaScript;

这样做没有意义,因为对象的属性没有顺序。JavaScript 不保证对象中的属性顺序;

Since ECMAScript 2015, using the Map object could be an alternative. A Map shares some similarities with an Object and guarantees the keys order:

从 ECMAScript 2015 开始,使用 Map 对象可能是一种替代方法。Map 与 Object 有一些相似之处并保证键顺序

回答by Asaleya

var reverseObj = function(object) {
    var NewObj = {}, keysArr = Object.keys(object);
    for (var i = keysArr.length-1; i >= 0; i--) {
        NewObj[keysArr[i]] = object[keysArr[i]];
    }
    return NewObj;
}