简体   繁体   English

jQuery.each中的反向对象

[英]Reverse object in jQuery.each

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 OUT:1640,1641,1642,......,1651

How to make it in reverse order (ex. 1651, 1650...)? 如何以相反的顺序(例如1651,1650 ......)?

As it is, you can't in any reliable manner. 事实上,你不能以任何可靠的方式。 Because you are enumerating an Object, there is never a guaranteed order. 因为您枚举了一个Object,所以从来没有保证订单。

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]);
    }
}

There is another solution, a fairly easy one: 还有另一个解决方案,一个相当简单的方法:

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

That's it. 而已。

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()”。

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);
});

For Objects 对象

If you are dealing with an object, reverse() won't work! 如果你正在处理一个对象,reverse()将无法正常工作! Instead you can do this to maintain the order. 相反,你可以这样做来维持秩序。

$.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!
});

jsonObj = []; jsonObj = [];

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

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]; });

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);
}

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'];
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM