Created
January 14, 2022 05:11
-
-
Save khle/78285b4d6917d3f24ec5e42fc14af25b to your computer and use it in GitHub Desktop.
Step 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ firstName: 'Jane', lastName: 'Foo' }, | |
{ firstName: 'John', lastName: 'Bar' }, | |
{ firstName: 'Jill', lastName: 'Err' } | |
] | |
function findMatchingAndMoveToFirst (users, shouldBeFirst) { | |
users.forEach((user, index) => { | |
if ( | |
user.firstName === shouldBeFirst.firstName && | |
user.lastName === shouldBeFirst.lastName | |
) { | |
const foundUsers = users.splice(index, 1) | |
users.unshift(foundUsers[0]) | |
} | |
}) | |
} | |
findMatchingAndMoveToFirst(users, { firstName: 'John', lastName: 'Bar' }) | |
console.log(users) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment