douchen2025 2019-06-02 18:19
浏览 108

用于更新JSON响应中元素位置的最快算法[关闭]

Considering the JSON structure bellow, I need to be able to change the position of an item. The position can be changed only in increments of 1 (so +1 or -1).

Given the fact that I always send back to the API the entire JSON and that there can be a few hundreds items in the structure, is there a known algorithm to achieve this positions change fast?

The item is searched in the structure by its id.

For example I want to update the "position" of item with "id = 2" to "1". This step is easy because I can loop through the structure and find the element by id. But where I got stuck is the step where I need to update the other items accordingly.

I'm looking to implement this in PHP or Javascript.

{
  "employees": {
    "employee": [
      {
        "id": "1",
        "firstName": "Tom",
        "lastName": "Cruise",
        "position": 1
      },
      {
        "id": "2",
        "firstName": "Maria",
        "lastName": "Sharapova",
        "position": 2
      },
      {
        "id": "3",
        "firstName": "Robert",
        "lastName": "Downey Jr.",
        "position": 3
      }
    ]
  }
}
  • 写回答

3条回答 默认 最新

  • dqnek0079 2019-06-02 18:51
    关注

    If you want to change the position of an element from i to j, you need to change the positions of the elements of which position is between i to j.

    var arr = [
      {
        "id": "1",
        "firstName": "Tom",
        "lastName": "Cruise",
        "position": 1
      },
      {
        "id": "2",
        "firstName": "Maria",
        "lastName": "Sharapova",
        "position": 2
      },
      {
        "id": "3",
        "firstName": "Robert",
        "lastName": "Downey Jr.",
        "position": 3
      }
    ]
    
    function changePosition(from, to) {
        var startValue;
        var endValue;
        for (var l = 0; l < arr.length; l++) {
        if (arr[l].position == from) {
            startValue = arr[l].position;
        }
        if (arr[l].position == to) {
            endValue = arr[l].position;
        }
      }
        var i = Math.min(startValue, endValue);
      var j = Math.max(startValue, endValue);
      var inc = from > to ? 1 : -1;
      for (var k = 0; k < arr.length; k++) {
        if (arr[k].position >= i && arr[k].position <= j) {
            if (arr[k].position == from) {
            arr[k].position = to;
          } else {
            arr[k].position += inc;
          }
        }
      }
    }
    
    changePosition(3, 1);
    console.log(arr);

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛