对JSON对象进行排序 [英] Sorting JSON Object

查看:112
本文介绍了对JSON对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试对JSON对象进行排序时遇到问题。基本上,人们可以按任意随机顺序将产品添加到我们的订单表单中,但是它在摘要中显示的顺序需要是我们希望它们如何定位(而不是他们选择它们的顺序),这就是我需要排序的原因'id'(或者我们稍后将按'pos'字段排序)

Having a problem trying to sort a JSON object. Basically, people can add products in any random order to our order form, but the order it shows in the summary needs to be how we want them to be positioned (not the order they select them), so thats why I need to sort by 'id' (or we'll sort by a 'pos' field later)

基本上,我需要按id升序排序。 1,2,103而不是2,103,1

Essentially, I need to sort by the id ascending. 1,2,103 instead of 2,103,1

我似乎遇到了问题,因为单个对象的索引是数字(或者只是它们在那里的事实.. 。)。

I seem to be having issues because the index into the individual objects are numbers (or just the fact that they're there...).

我需要按照array.sort(function(a,b){return a.id-b.id})的方式做一些事情;但我认为这不起作用,因为1,它不是一个数组(它是一个对象),2,它有那些讨厌的索引(我需​​要代码的另一部分)......

I need to do something along the lines of array.sort(function(a,b){ return a.id-b.id }); but I'm presuming that doesn't work because 1, its not an array (its an object), and 2, it has those pesky indexes (that i need for another part of my code)...

任何想法????

var products = {
    "2": {
        "id": "2",
        "price": "119",
        "quantity": "1",
        "thumb": "img\/store\/comp-08n.png"
    },
    "103": {
        "id": "103",
        "price": "109",
        "quantity": "1",
        "thumb": "img\/store\/basketballhoop.png"
    },
    "1": {
        "id": "1",
        "price": "309",
        "quantity": "1",
        "thumb": "img\/store\/comp-08.png"
    }
};


推荐答案

您的订单中需要多少件商品?您可以安全地在Javascript阵列中对10'000个项目进行排序,而不会出现太多速度问题。你为什么不使用真正的数组呢?

How many items do you need in your orders? You can safely sort 10'000 items in a Javascript array without much of speed issues. Why don't you work with a real array instead?

你甚至可以为它注入自定义属性,大致类似于

You could even inject custom properties to it, roughly something like

var products = [...];

products.findById = function(id) {
   for (var i=0, len=this.length; i<len; i++) {
      if (id == this[i].id) return this[i];
   }
   return null;
};

alert( products.findById(103).price );   // -> 119

并添加预定义的分拣机,如

and add predefined sorters like

products.sortById = function() {
    this.sort(function(a,b) {
        return a.id - b.id;
    });
};

products.sortById();   // sort array by product id

** 编辑 **

在你的PHP方面,你可能有类似的东西:

On your PHP side, you might have something like :

$products = array(
    2 => array( 'id' => 2, ... ),
    103 => array( 'id' => 103, ... ),
    1 => array( 'id' => 1, ... ),
);

// get a JSON array
$jsonArray = json_encode(array_values($products));

将返回您所需的信息。

** 注意 **

在阵列中添加新项目时,不应显式设置索引。使用数组的 push 函数,例如

You should not explicitly set indexes when adding new items in your array. Use the array's push function, like

products.push({id:123, price:200.49, quantity:1, thumb:'/path/to/file'});

删除项目有点棘手,例如:

Removing an item is a bit tricky, however, something like :

products.removeById = function(id) {
   for (var i=0, len=this.length; i<len; i++) {
      if (id == this[i].id) return this.splice(i, 1)[0];
   }
   return null;
};

products.removeById(123);    // -> returns the removed element! or null if nothing was removed

参见demo 这里(使用Chrome开发工具进行控制台输出)。

See demo here (use Chrome developper tools for console output).

这篇关于对JSON对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆