2

Considering I have the following JSON, how could I generate another JSON, ordering by a field (let's say anoCobranca)?

{
  "extratos": [
    {
      "descricao":         "product A",
      "anoCobranca":       2012,
      "mesCobranca":       01,
      "nomeMesCobranca":   "Janeiro",
      "diaUsoInicial":     1,
      "diaUsoFinal":       30,
      "valor":             "236.81",
      "vencimento":        "24/01/2012",
      "formaPagamento":    "Cartão",
      "situacao": "ok"
    },
    {
      "descricao":         "product B",
      "anoCobranca":       2012,
      "mesCobranca":       2,
      "nomeMesCobranca":   "Fevereiro",
      "diaUsoInicial":     1,
      "diaUsoFinal":       29,
      "valor":             "249.81",
      "vencimento":        "24/02/2012",
      "formaPagamento":    "-",
      "situacao":          "aberto"
    },
    {
      "descricao":         "product 1",
      "anoCobranca":       2012,
      "mesCobranca":       3,
      "nomeMesCobranca":   "Março",
      "diaUsoInicial":     1,
      "diaUsoFinal":       31,
      "valor":             "339.11",
      "vencimento":        "24/03/2012",
      "formaPagamento":    "Cartão",
      "situacao":          "ok"
    },
    {
      "descricao":         "product D",
      "anoCobranca":       2011,
      "mesCobranca":       4,
      "nomeMesCobranca":   "Abril",
      "diaUsoInicial":     1,
      "diaUsoFinal":       30,
      "valor":             "119.18",
      "vencimento":        "24/04/2012",
      "formaPagamento":    "Cartão",
      "situacao":          "ok"
    },
    {
      "descricao":         "product E",
      "anoCobranca":       2011,
      "mesCobranca":       5,
      "nomeMesCobranca":   "Maio",
      "diaUsoInicial":     1,
      "diaUsoFinal":       30,
      "valor":             "81.29",
      "vencimento":        "24/05/2012",
      "formaPagamento":    "-",
      "situacao":          "aberto"
    }
  ]
}

I've tried a few techniques like:

function sortJSON(a,b){
  return parseInt(a.json.extratos.anoCobranca - b.json.extratos.anoCobranca)
}

And a few others I found here, at StackOverflow, but all of them failed.

4

3 回答 3

5

Sort the array with a callback function :

obj.extratos.sort(function(a, b) {
  return a.anoCobranca - b.anoCobranca;
});
于 2012-04-13T12:42:45.953 回答
1

使用下划线,您可以:

_(obj.extratos).pluck(propertyName).sort() 
于 2012-04-13T12:53:24.527 回答
1

如果您不反对使用外部库,那么我建议您使用Underscore.js来解决这个问题和类似问题。这是我在下面一行中执行此操作的示例:

var model = {
  "extratos": [
    {
      "descricao":         "product A",
      "anoCobranca":       2012,

            .
            .
            .

      "situacao":          "aberto"
    }
  ]
};

model.extratos = _.sortBy(model.extratos, 
                   function (extrato) { return extrato.anoCobranca; });

console.log(model);

这是一个 jsFiddle,您可以在其中玩:http: //jsfiddle.net/JohnMunsch/4eE2F/

Yanick 的解决方案也是一个不错的解决方案,但我认为 Underscore 库具有广泛的功能(例如 map 和 reduce),可以帮助您解决一组复杂的数据操作问题,而不仅仅是简单的排序。

于 2012-04-13T12:49:59.447 回答