Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort changes/edited list of object data in array

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.

like image 777
sg552 Avatar asked Aug 26 '26 16:08

sg552


2 Answers

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; }
like image 94
Nina Scholz Avatar answered Aug 29 '26 08:08

Nina Scholz


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.

like image 40
Mouser Avatar answered Aug 29 '26 08:08

Mouser