Я хочу отфильтровать данные GeoJSON на основе объекта, созданного на основе выбора, сделанного в раскрывающемся меню с несколькими выборами.
Данные GeoJSON
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -115.55783329999998,32.9646667 ]
},
"properties": { "magType":"mb", "type":"earthquake","horizontalError":0.32,"depthError":0.58, "city":"Brawley", "state":"California", "country":"US"}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -115.54583329999998,32.98 ]
},
"properties": { "magType":"mb", "type":"earthquake", "horizontalError":0.24, "depthError":0.46, "city":"Brawley", "state":"California", "country":"US"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -118.13383329999999,33.777333299999995 ]
},
"properties": {"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9, "city":"Brawley","state":"California","country":"US"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -115.555,32.967 ]
},
"properties": {"magType":"ml","type":"earthquake", "horizontalError":0.43, "depthError":0.67, "city":"Isangel","state":"Tafea","country":"VU"
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -115.55216670000001,32.9658333 ]
},
"properties": {"magType":"mw","type":"tsunami", "horizontalError":0.79, "depthError":1.35, "city":"Zaybak", "state":"Badakhshan", "country":"AF"
}
},
Объект выбранных значений:
sel_data_category = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']}
sel_data_quant ={horizontalError:[0.68,0.90]}
Я хочу фильтровать данные на основе этих выбранных значений. Таким образом, ожидаемый результат должен быть -
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -115.55216670000001,32.9658333 ]
},
"properties": {"magType":"mw","type":"tsunami", "horizontalError":0.79, "depthError":1.35, "city":"Zaybak", "state":"Badakhshan", "country":"AF"
}
}
Есть ли способ сделать это?
EDIT: отсутствует значение горизонтальной стоимости
Вы можете использовать Array.filter
РЕШЕНИЕ 1: Учетные параметры фильтра
let arr = [{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55783329999998,32.9646667]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.32,"depthError":0.58,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.54583329999998,32.98]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.24,"depthError":0.46,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-118.13383329999999,33.777333299999995]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.555,32.967]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.43,"depthError":0.67,"city":"Isangel","state":"Tafea","country":"VU"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55216670000001,32.9658333]},"properties":{"magType":"mw","type":"tsunami","horizontalError":0.79,"depthError":1.35,"city":"Zaybak","state":"Badakhshan","country":"AF"}}];
let filterObj = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']};
let result = arr.filter(o => filterObj.country.includes(o.properties.country)
&& filterObj.city.includes(o.properties.city)
&& filterObj.magType.includes(o.properties.magType));
console.log(result);
РЕШЕНИЕ 2: общие фильтры фильтров (все значения в массиве)
let arr = [{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55783329999998,32.9646667]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.32,"depthError":0.58,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.54583329999998,32.98]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.24,"depthError":0.46,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-118.13383329999999,33.777333299999995]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.555,32.967]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.43,"depthError":0.67,"city":"Isangel","state":"Tafea","country":"VU"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55216670000001,32.9658333]},"properties":{"magType":"mw","type":"tsunami","horizontalError":0.79,"depthError":1.35,"city":"Zaybak","state":"Badakhshan","country":"AF"}}];
let filterObj = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']};
let filterObjArray = Object.entries(filterObj);
let result = arr.filter(o => filterObjArray.every(([k,v]) => v.includes(o.properties[k])));
console.log(result);
РЕДАКТИРОВАТЬ
let arr = [{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55783329999998,32.9646667]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.32,"depthError":0.58,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.54583329999998,32.98]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.24,"depthError":0.46,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-118.13383329999999,33.777333299999995]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.555,32.967]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.43,"depthError":0.67,"city":"Isangel","state":"Tafea","country":"VU"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55216670000001,32.9658333]},"properties":{"magType":"mw","type":"tsunami","horizontalError":0.79,"depthError":1.35,"city":"Zaybak","state":"Badakhshan","country":"AF"}}];
let sel_data_category = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']}
let sel_data_quant ={horizontalError:[0.68,0.90]}
let filterObjArray = Object.entries(sel_data_category);
let filterQuantArray = Object.entries(sel_data_quant);
let result = arr.filter(o => filterObjArray.every(([k,v]) => v.includes(o.properties[k])) && filterQuantArray.every(([k,[l,h]]) => o.properties[k] >= l && o.properties[k] <= h));
console.log(result);
filterObj
т.е. вы можете группировать по категориям то, что должно находиться в диапазоне или соответствовать точному значению из массива и / или некоторым другим критериям фильтра? Потому что, если вы ищете решение 2, то должен быть какой-то общий способ разделения типов