【问题标题】:How to reposition JSON structure key and value?如何重新定位 JSON 结构的键和值?
【发布时间】:2018-01-16 15:30:18
【问题描述】:

我有一个这样的 json:

[
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 1
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 0
    }
]

我想重新定位json的结构以防hasil = 1到最后一个位置,并保留seq值,

在这里问之前我已经重新搜索过,很遗憾我不知道这个问题的正确关键字。

目的:

[
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 1
    }
]

有办法让它成为可能吗?

【问题讨论】:

标签: javascript json


【解决方案1】:

只需 sort 使用用户功能的数组

var array = [
    {
        "tipe": "foo1",
        "color": "red",
        "size": 92,
        "seq": 1,
        "hasil": 0
    },
    {
        "tipe": "foo2",
        "color": "red",
        "size": 92,
        "seq": 2,
        "hasil": 1
    },
    {
        "tipe": "foo3",
        "color": "red",
        "size": 92,
        "seq": 3,
        "hasil": 0
    }
];

array.sort(function (a, b) {
  return a.hasil - b.hasil;
});

for (var i = 0; i < array.length; i++) {
  array[i].seq = i+1;
}

console.log(array);

【讨论】:

  • OP 希望在同一索引处保留 seq 值。
  • 问题在于seq,我需要将seq保持在与第一个json相同的位置,
  • @flix 检查更新。排序后重新迭代您的数组并重新分配 seq
  • @flix 您能否验证您的原始 seq 值是否始终有序且不会丢失任何值(如 1、2、4、5、7)?
  • @gurvinder372 真的,我有6个json索引,这个seq就像一个排名,虽然答案是对的,我先试试
【解决方案2】:

您可以创建一个包含hasil 值的数组并对其进行排序,然后使用array#mapobject#assign 创建您的新数组。

var input = [{ "tipe": "foo1", "color": "red", "size": 92, "seq": 1, "hasil": 0 }, { "tipe": "foo2", "color": "red", "size": 92, "seq": 2, "hasil": 1 }, { "tipe": "foo3", "color": "red", "size": 92, "seq": 3, "hasil": 0 }],
    sortedHasil = input.map(({hasil}) => hasil).sort(),
    result = input.map((o, i) => Object.assign(o, {'hasil': sortedHasil[i]}));
console.log(result);

【讨论】:

    【解决方案3】:

    hasil 对数组进行排序,然后循环遍历它以重新分配seq

    var arr = [
        {
            "tipe": "foo1",
            "color": "red",
            "size": 92,
            "seq": 1,
            "hasil": 0
        },
        {
            "tipe": "foo2",
            "color": "red",
            "size": 92,
            "seq": 2,
            "hasil": 1
        },
        {
            "tipe": "foo3",
            "color": "red",
            "size": 92,
            "seq": 3,
            "hasil": 0
        }
    ];
       
    var seqs = arr.map(e => e.seq);
    arr.sort((a, b) => a.hasil - b.hasil).forEach((e, i) => e.seq = seqs[i]);
    console.log(arr);

    【讨论】:

    • OP 希望将 seq 值保留在同一索引处
    【解决方案4】:

    尝试排序功能 如果你不想排序,你可以用 if else 来做

    var input =[
        {
            "tipe": "foo1",
            "color": "red",
            "size": 92,
            "seq": 1,
            "hasil": 0
        },
        {
            "tipe": "foo3",
            "color": "red",
            "size": 92,
            "seq": 2,
            "hasil": 0
        },
        {
            "tipe": "foo2",
            "color": "red",
            "size": 92,
            "seq": 3,
            "hasil": 1
        }
    ]
    
    input.sort(function (a,b){
    
        return a.hasil - b.hasil
    
    
    })
    

    【讨论】:

      【解决方案5】:

      你可以有一个排序,然后一个映射函数来处理这个,比如

      var json = [
          {
              "tipe": "foo1",
              "color": "red",
              "size": 92,
              "seq": 1,
              "hasil": 0
          },
          {
              "tipe": "foo2",
              "color": "red",
              "size": 92,
              "seq": 2,
              "hasil": 1
          },
          {
              "tipe": "foo3",
              "color": "red",
              "size": 92,
              "seq": 3,
              "hasil": 0
          }
      ]
      
      
      var newArr = json.sort(function(a, b){
        if(a.hasil === b.hasil)
            return a.seq  - b.seq;
      
        return a.hasil - b.hasil
      }).map(function(a, i) {
        return {
          ...a,
          seq: i+1
        }
      })
      
      console.log(newArr)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 2016-08-19
        • 1970-01-01
        • 1970-01-01
        • 2017-07-15
        • 1970-01-01
        • 2021-12-14
        相关资源
        最近更新 更多