- Creating Extractor instance and test data
- Getting single values
2.1. ... if path exists
2.2. ... if path doesn't exist
2.3. ... with checking of path existence - Getting arrays of values
3.1. ... if path exists
3.2. ... if path doesn't exist
3.3. ... with checking of path existence - More examples
4.1. Getting of array elements
4.2. Getting a defined property of array elements (objects)
4.3. Getting a defined property of array elements (objects) that are deeply nested - Using filters
5.1. Getting names of countries with population bigger than 300 millions
5.2. Getting names of cities where name starts with ‘S’
5.3. Getting cities with population bigger than 20 millions in countries with population bigger than 1 billion
const Extractor = require('prop-extractor');
let extractor = new Extractor();
let data = {
countries: [
{
name: 'Australia',
population: 24641662,
cities: [
{name: 'Sydney', population: 5029768},
{name: 'Melbourne', population: 4725316},
{name: 'Canberra', population: 435019}
]
},
{
name: 'Brazil',
population: 211243220,
cities: [
{name: 'São Paulo', population: 12038175},
{name: 'Rio de Janeiro', population: 6498837},
{name: 'Salvador', population: 2938092}
]
},
{
name: 'China',
population: 1388232693,
cities: [
{name: 'Shanghai', population: 24152700},
{name: 'Beijing', population: 21700000},
{name: 'Guangzhou', population: 14043500}
]
},
{
name: 'United States',
population: 326474013,
cities: [
{name: 'New York', population: 8537673},
{name: 'Los Angeles', population: 3976322},
{name: 'Chicago', population: 2704958}
]
}
],
country_codes: ['AU', 'BR', 'CN', 'US'],
count: 4
};
extractor.setPath('data.count');
console.log(extractor.extractFrom(data).get());
// 4
extractor.setPath('data.foo.bar.baz');
console.log(extractor.extractFrom(data).get());
// undefined
extractor.setPath('data.count');
let response = extractor.extractFrom(data);
if (response.has()) {
console.log(response.get());
}
// 4
extractor.setPath('data.country_codes.[]');
console.log(extractor.extractFrom(data).get());
// ['AU', 'BR', 'CN', 'US']
extractor.setPath('data.foo.[].bar');
console.log(extractor.extractFrom(data).get());
// []
extractor.setPath('data.country_codes.[]');
response = extractor.extractFrom(data);
if (response.has()) {
console.log(response.get());
}
// ['AU', 'BR', 'CN', 'US']
extractor.setPath('data.countries.[]');
console.log(extractor.extractFrom(data).get());
// [ { name: 'Australia', population: 24641662, cities: [ ... ] },
// { name: 'Brazil', population: 211243220, cities: [ ... ] },
// { name: 'China', population: 1388232693, cities: [ ... ] },
// { name: 'United States', population: 326474013, cities: [ ... ] } ]
extractor.setPath('data.countries.[].name');
console.log(extractor.extractFrom(data).get());
// [ 'Australia', 'Brazil', 'China', 'United States' ]
extractor.setPath('data.countries.[].cities.[].name');
console.log(extractor.extractFrom(data).get());
// [ 'Sydney', 'Melbourne', 'Canberra', 'São Paulo',
// 'Rio de Janeiro', 'Salvador', 'Shanghai', 'Beijing',
// 'Guangzhou', 'New York', 'Los Angeles', 'Chicago' ]
extractor.setPath('data.countries.[population].name');
extractor.removeAllFilters();
extractor.setFilter('population', (item) => {
return (item.population > 300000000);
});
console.log(extractor.extractFrom(data).get());
// [ 'China', 'United States' ]
extractor.setPath('data.countries.[].cities.[city_name].name');
extractor.removeAllFilters();
extractor.setFilter('city_name', (item) => {
return (item.name[0] === 'S');
});
console.log(extractor.extractFrom(data).get());
// [ 'Sydney', 'São Paulo', 'Salvador', 'Shanghai' ]
5.3. Getting cities with population bigger than 20 millions in countries with population bigger than 1 billion
extractor.setPath('data.countries.[country_population].cities.[city_population]');
extractor.removeAllFilters();
extractor.setFilter('country_population', (item) => {
return (item.population > 1000000000);
});
extractor.setFilter('city_population', (item) => {
return (item.population > 20000000);
});
console.log(extractor.extractFrom(data).get());
// [ { name: 'Shanghai', population: 24152700 },
// { name: 'Beijing', population: 21700000 } ]