Skip to content

Instantly share code, notes, and snippets.

@khle
Created January 14, 2022 06:06
Show Gist options
  • Save khle/4088eb1071788da35c0928e4e7ea9838 to your computer and use it in GitHub Desktop.
Save khle/4088eb1071788da35c0928e4e7ea9838 to your computer and use it in GitHub Desktop.
Step 2
const users = [
{ firstName: 'Jane', lastName: 'Foo' },
{ firstName: 'John', lastName: 'Bar' },
{ firstName: 'Jill', lastName: 'Err' }
]
function findMatchingAndMoveToFirst (users, shouldBeFirst) {
const reArrangedUsers = []
users.forEach(user => {
if (
user.firstName === shouldBeFirst.firstName &&
user.lastName === shouldBeFirst.lastName
) {
reArrangedUsers.unshift(user)
} else {
reArrangedUsers.push(user)
}
})
return reArrangedUsers
}
const reArrangedUsers = findMatchingAndMoveToFirst(users, {
firstName: 'John',
lastName: 'Bar'
})
console.log(reArrangedUsers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment