Skip to content

Instantly share code, notes, and snippets.

@khle
Created January 14, 2022 06:51
Show Gist options
  • Save khle/74e7932e2294fc3f07640e4cd61fbef9 to your computer and use it in GitHub Desktop.
Save khle/74e7932e2294fc3f07640e4cd61fbef9 to your computer and use it in GitHub Desktop.
Step 5
const users = [
{ firstName: 'Jane', lastName: 'Foo' },
{ firstName: 'John', lastName: 'Bar' },
{ firstName: 'Jill', lastName: 'Err' }
]
function findMatchingAndMoveToFirst (users, shouldBeFirst) {
return users.reduce(
(accumulator, current) =>
current.firstName === shouldBeFirst.firstName &&
current.lastName === shouldBeFirst.lastName
? [current, ...accumulator]
: [...accumulator, current],
[]
)
}
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