I am not 100% sure what you are looking for but if you are looking for a new object with each country name as the property and the population you can try.
var countries = {
China: 1371980000,
India: 1276860000,
'United States': 321786000,
Indonesia: 255461700,
Brazil: 204873000,
Pakistan: 190860000
};
var countriesFiltered = {};
Object.keys(countries).filter(function(key) {
return countries[key] <= 1000000000;
}).map(function(key) {
countriesFiltered[key] = countries[key];
});
console.log(countriesFiltered);
This results in:
{
United States: 321786000,
Indonesia: 255461700,
Brazil: 204873000,
Pakistan: 190860000
}
If you want an array of Key Value Pairs you could try.
var countries = {
China: 1371980000,
India: 1276860000,
'United States': 321786000,
Indonesia: 255461700,
Brazil: 204873000,
Pakistan: 190860000
};
var countriesFiltered = Object.keys(countries).filter(function(key) {
return countries[key] <= 1000000000;
}).map(function(key) {
return { country: key, population: countries[key]};
});
console.log(countriesFiltered);
EDIT: Answer to comment
what does mean exactly, reausable, where i can read about that ?
If you review @answer-38909742 you will see that they have modified the Object
type and added a static method Object.filter()
with the signature of Object.filter = (obj, predicate) => {}
This then makes it reusable and not limited to the scope of the function but to any object.
If this was placed in a global script somewhere in your application you can then resuse this method on any object in any scope.
As stated in his question simply by calling.
Object.filter(countries, countrie => countrie <= 1000000000);