Skip to content

Instantly share code, notes, and snippets.

@khle
Created January 14, 2022 05:11
Show Gist options
  • Save khle/78285b4d6917d3f24ec5e42fc14af25b to your computer and use it in GitHub Desktop.
Save khle/78285b4d6917d3f24ec5e42fc14af25b to your computer and use it in GitHub Desktop.
Step 1
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