Created
January 14, 2022 06:20
-
-
Save khle/b7f034f09013249404ad6ce16693cadb to your computer and use it in GitHub Desktop.
Step 3
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) { | |
return users.reduce((accumulator, current) => { | |
if ( | |
current.firstName === shouldBeFirst.firstName && | |
current.lastName === shouldBeFirst.lastName | |
) { | |
return [current, ...accumulator] | |
} else { | |
return [...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