Javascript jQuery.each 中的反向对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4838630/
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 14:20:00  来源:igfitidea点击:

Reverse object in jQuery.each

javascriptjqueryobject

提问by Sladex

HTML:

HTML:

<input id="sdata" type="hidden" value='{"1651":["12","1"],"1650":["30","0"],"1649":["20","0"],"1648":["13","2"],"1647":["11","0"],"1646":["10","0"],"1645":["12","0"],"1644":["8","0"],"1643":["16","1"],"1642":["10","1"],"1641":["10","0"],"1640":["18","3"]}' />

JS:

JS:

var data = $.parseJSON($('#sdata').val());
$.each(data, function(id, sc) {
    alert(id);
}

OUT: 1640, 1641, 1642, ..., 1651

输出: 1640, 1641, 1642, ..., 1651

How to make it in reverse order (ex. 1651, 1650...)?

如何以相反的顺序制作(例如 1651、1650...)?

回答by user113716

As it is, you can't in any reliable manner. Because you are enumerating an Object, there is never a guaranteed order.

事实上,你不能以任何可靠的方式。因为您正在枚举一个对象,所以永远没有保证的顺序。

If you want a guaranteed numeric order, you would need to use an Array, and iterate backwards.

如果你想要一个有保证的数字顺序,你需要使用一个数组,然后向后迭代。



EDIT:This will convert your Object to an Array, and do a reverse iteration.

编辑:这会将您的对象转换为数组,并进行反向迭代。

Note that it will only work if all the properties are numeric.

请注意,它仅在所有属性都是数字时才有效。

var data = $.parseJSON($('#sdata').val());
var arr = [];

for( var name in data ) {
    arr[name] = data[name];
}
var len = arr.length;
while( len-- ) {
    if( arr[len] !== undefined ) {
        console.log(len,arr[len]);
    }
}

回答by xorinzor

There is another solution, a fairly easy one:

还有另一种解决方案,一个相当简单的解决方案:

$(yourobject).toArray().reverse();

That's it.

就是这样。

回答by Araya Yamir

I tried this and it worked perfectly for me.

我试过这个,它对我来说非常有效。

var data = $.parseJSON($('#sdata').val());
$.each(data.reverse(), function(id, sc) {
    alert(id);
});

The only change is the "reverse()" in line 2.

唯一的变化是第 2 行中的“reverse()”。

回答by Alex K.

If all you need to do is generate some HTML out of your JSON and put generated elements into a container in reverse order you can use jQuery's prependTo method when building your HTML.

如果您需要做的只是从 JSON 中生成一些 HTML 并将生成的元素以相反的顺序放入容器中,您可以在构建 HTML 时使用 jQuery 的 prependTo 方法。

var container = $('<div />');
$.each(data, function (key, value) {
    $('<div>' + value + '</div>').prependTo(container);
});

回答by Kareem

For Objects

对于对象

If you are dealing with an object, reverse() won't work! Instead you can do this to maintain the order.

如果您正在处理一个对象, reverse() 将不起作用!相反,您可以这样做来维护订单。

$.each(Object.keys(myObj).reverse(),function(i,key){
  var value = myObj[key];
  //you have got your key and value in a loop that respects the order, go rock!
});

回答by Ivan

You can use javascript function sort() or reverse()

您可以使用 javascript 函数 sort() 或 reverse()

var data = $.parseJSON($('#sdata').val());
data.reverse();
$.each(data, function(id, sc) {
alert(id);
}

回答by BentFX

I don't know, but for the simple stuff I work with, this function does the job. It doesn't rely on numeric keys. And will flip simple objects top to bottom. I don't understand complex Objects, so I don't know how "robust" it is.

我不知道,但是对于我使用的简单东西,这个函数可以完成工作。它不依赖于数字键。并将从上到下翻转简单的对象。我不懂复杂的对象,所以我不知道它有多“健壮”。

function flipObject(obj){
    var arr = [];
    $.each(obj, function(key, val){
        var temp = new Object;
        temp['key'] = key;
        temp['val'] = val;
        arr.push(temp);
        delete temp;
        delete obj[key];
    });
    arr.reverse();
    $.each(arr, function(key, val){
        obj[val['key']] = val['val'];
    });
}

回答by Muhammad Bilal

jsonObj = [];

jsonObj = [];

            $.each(data.reverse(), function (i, dt) {
                jsonObj.push({
                    'id': dt.id
                });

回答by Robert McMahan

Here's an option that seemed to have worked for me. Hope this helps someone. Obviously only works on simple objects (non-nested) but I suppose you could figure out way to make something more complicated with a little extra work.

这是一个似乎对我有用的选项。希望这可以帮助某人。显然只适用于简单的对象(非嵌套),但我想你可以想办法让一些额外的工作变得更复杂。

var reversed_object = {};
Object.keys(original_object).reverse().forEach(function(key)
{ reversed_object[key] = original_object[key]; });