var cities = [
{ name: "London", "population": 8615246 },
{ name: "Berlin", "population": 3517424 },
{ name: "Madrid", "population": 3165235 },
{ name: "Rome", "population": 2870528 }
];
var jp = jsonpath; // для краткости
var names = jp.query(cities, '$..name');
console.log(names); // [ "London", "Berlin", "Madrid", "Rome" ]
const sourceKey = 'population';
const sourceVal = 2870528;
const targetKey = 'name';
const [ val ] = jp.query(cities, `$[?(@.${sourceKey} == ${sourceVal})].${targetKey}`);
const val = (cities.find(n => n[sourceKey] === sourceVal) || {})[targetKey];
$[?(@.population == 2870528)].name
?( условие )
{
"cities": [
{ "name": "London", "population": 8615246 },
{ "name": "Berlin", "population": 3517424 },
{ "name": "Madrid", "population": 3165235 },
{ "name": "Rome", "population": 2870528 }
]
}
$.cities[?(@.population == 8615246)] }
[
{
"name": "London",
"population": 8615246
}
]