Sort changes/edited list of object data in array

Question

Let's say I have this object:

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

and then I change description in id 30 from hmmm to:

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

and it will become like this:

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

My question is, how can I changes/sort after I edited the object? I want the recent edited object at the top like this:

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

I also using Lodash library, if anyone know any example with lodash or native please refer me to it. Thanks in advance.


Show source
| javascript   | arrays   | object   | lodash   | sorting   2017-01-03 15:01 3 Answers

Answers to Sort changes/edited list of object data in array ( 3 )

  1. 2017-01-03 15:01

    You could search for the item with the wanted id with Array#some, splice it and unshift the item to the array.

    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; }

  2. 2017-01-03 15:01

    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");

    A possible solution to your problem, using forEach to change and splice and unshift to manipulate.

  3. 2017-01-03 18:01

    All this reply gave me some idea. Here's how you do it with lodash. You must know id object that you just updated.

    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)
    

    Example : https://fiddle.jshell.net/uLqfuqn1/

    Reference : https://lodash.com/docs/4.17.4#remove

    Thanks to all that reply.

Leave a reply to - Sort changes/edited list of object data in array

◀ Go back