Sort the object array and return the next object?

let arr = [{id:1,name:1,job:1}, {id:2,name:2,job:2}, {id:4,name:4,job:4}, {id:3,name:3,job:3}.] ;

suppose: the value returned to me by the backend is {id:2,name:2,job:2};
then what can I do to get the {id:3,name:3,job:3} object? (that is, after the current object is returned in the background, the next object is sorted)
Thank you!

Mar.22,2022

 
 id:
 arr.sort(function (a, b) {
     return a.id - b.id;
 })
 

there is a solution, but what I want to ask is, since the backend can return {id:2,name:2,job:2} to you, why can't it return {id:3,name:3,job:3} directly to you? The value logic based on the front end may be unreliable, and it is generally better to do it at the back end.


assume that the data on the backend is:

const data = {id: 3, name: 3, job: 3}

the next object to get is:

let result = arr.find(item => item.id === (data.id + 1))

you can replace item.id and data.id by which field you need as the comparison attribute. This method does not depend on sorting.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35f88-2b82b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35f88-2b82b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?