Created
October 14, 2018 19:43
-
-
Save shekohex/b35a5d91766de612b0d9fc552b5326a7 to your computer and use it in GitHub Desktop.
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 = [ | |
{ | |
Name: 'Ramy', | |
City: { | |
1: { | |
city: 'Zagazig' | |
}, | |
2: { | |
city: 'Banha' | |
} | |
} | |
}, | |
{ | |
Name: 'Ahmed', | |
City: { | |
1: { | |
city: 'Mansoura' | |
}, | |
2: { | |
city: 'Cairo' | |
} | |
} | |
} | |
]; | |
const filteredUsers = users | |
.map(u => { | |
// get only the city names of users City object | |
const cityNames = Object.values(u.City).map(c => c.city); | |
// make a copy of that user, we don't love mutate data. | |
// but only update City prop to be city names. | |
const user = { ...u, City: cityNames }; | |
return user; | |
}) | |
// Then we filter users where City have `Zagazig` | |
.filter(u => u.City.includes('Zagazig')); // => [ { Name: 'Ramy', City: [ 'Zagazig', 'Banha' ] } ] | |
console.log(filteredUsers); | |
// to make sure that orignal data is safe | |
console.log(users); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment