Array = [
{id: 0, name: name, email: email},
{id: 1, name: name, email: email},
{id: 2, name: name, email: email}, ...,
{id: n, name: name, email: email}]
const maxId = arr.reduce((max, item) => item.id > max ? item.id : max, 0);
const arr = [
{id: 3, name: 'name', email: 'email'},
{id: 0, name: 'name', email: 'email'},
{id: 2, name: 'name', email: 'email'},
{id: 1, name: 'name', email: 'email'}];
const max = (p, v) => {
if (p.id < v.id)
return -1;
if (p.id > v.id)
return 1;
return 0;
};
const addUser = (userObject) => {
arr.sort(max);
const maxId = arr[arr.length - 1]['id'];
userObject['id'] = maxId + 1;
arr.push(userObject);
};
addUser({name: 'new name', email: 'new email'});
console.log(arr);