对数组中对象数据的更改/编辑列表进行排序

时间:2017-01-03 14:10:27

标签: javascript arrays sorting object lodash

我们说我有这个对象:

{
  "data": {
    "result": [
      {
        "id": 25,
        "lineNo": "222",
        "description": "hello"
      },
      {
        "id": 30,
        "lineNo": "765",
        "description": "hmmm"
      },
      {
        "id": 31,
        "lineNo": "112",
        "description": "last"
      }
    ]
  }
}

然后我将ID 30中的descriptionhmmm更改为:

  {
    "id": 30,
    "lineNo": "765",
    "description": "should be first"
  },

它会变成这样:

{
  "data": {
    "result": [
      {
        "id": 25,
        "lineNo": "222",
        "description": "hello"
      },
      {
        "id": 30,
        "lineNo": "765",
        "description": "should be first"
      },
      {
        "id": 31,
        "lineNo": "112",
        "description": "last"
      }
    ]
  }
}

我的问题是,在编辑对象后如何更改/排序?我希望最近编辑的对象位于顶部,如下所示:

{
  "data": {
    "result": [
      {
        "id": 30,
        "lineNo": "765",
        "description": "should be first"
      },
      {
        "id": 25,
        "lineNo": "222",
        "description": "hello"
      },
      {
        "id": 31,
        "lineNo": "112",
        "description": "last"
      }
    ]
  }
}

我也使用Lodash库,如果有人知道lodash或native的任何例子请转介我。提前谢谢。

3 个答案:

答案 0 :(得分:2)

您可以使用Array#somespliceunshift项目搜索包含所需id的项目。



function updateArrayAndMove(array, id, key, value) {
    array.some(function (o, i, a) {
        if (o.id === id) {
            o[key] = value;
            a.unshift(a.splice(i, 1)[0]);
            return true;
        }
    });
}

var object = { data: { result: [{ id: 25, lineNo: "222", description: "hello" }, { id: 30, lineNo: "765", description: "hmmm" }, { id: 31, lineNo: "112", description: "last" }] } };

updateArrayAndMove(object.data.result, 30, 'description', 'should be first');
console.log(object);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:1)

var object =
{
  "data": {
    "result": [
      {
        "id": 25,
        "lineNo": "222",
        "description": "hello"
      },
      {
        "id": 30,
        "lineNo": "765",
        "description": "should be first"
      },
      {
        "id": 31,
        "lineNo": "112",
        "description": "last"
      }
    ]
  }
}

function changeElement(id, key, value)
{
  var changedId = -1;
  var arrReturned = object.data.result.forEach(function(element, index){
    if (element['id'] == id)
    {
      element[key] = value;
      changedId = index; //push the changed index into the array
    }
  });
  
  //element has changed
  //use splice to change the order
  var element = object.data.result.splice(changedId, 1);
  object.data.result.unshift(element[0])
  
  console.log(object)
}

changeElement(30, "description", "should be first");

问题的可能解决方案,使用forEach进行更改和拼接以及非移位操作。

答案 2 :(得分:0)

所有这些回复都给了我一些想法。这是你用lodash做的。您必须知道刚更新的id对象。

var object =
    {
      "data": {
        "result": [
          {
            "id": 25,
            "lineNo": "222",
            "description": "hello"
          },
          {
            "id": 30,
            "lineNo": "765",
            "description": "should be first"
          },
          {
            "id": 31,
            "lineNo": "112",
            "description": "last"
          }
        ]
      }
    }

    var filteredObj = _.remove(object.data.result, function(o) {
        return o.id == '30'; // must know object id
    });
    object.data.result.unshift(filteredObj[0]);

    console.log("mutated object.data.result", object.data.result)

示例:https://fiddle.jshell.net/uLqfuqn1/

参考:https://lodash.com/docs/4.17.4#remove

感谢所有回复。