[英]How to Extract data based on the values in one array after matching the corresponding values from another array in JavaScript?
This is the URL from GeoServer to get feature info这是来自 GeoServer 的 URL 以获取功能信息
{"type":"FeatureCollection","features":[{"type":"Feature","id":"weather_warning_day_1.fid--418ec0da_178b69d5dfc_-715c","geometry":null,"properties":{"issue_date":"2021-04-09","updated_at":"2021-04-09T09:26:33+05:30","utc_time":0,"state_name":"Odisha","state_id":21,"district_name":"MAYURBHANJ","district_id":232,"api_district_name":"MAYURBHANJ","day_1":"6,9,10","day1_color":3}}],"totalFeatures":"unknown","numberReturned":1,"timeStamp":"2021-04-09T15:38:19.536Z","crs":null}
the data I want to extract is of variable: "day_1":"6,9,10" which I got from the layer and stored it in the variable as我要提取的数据是变量:“day_1”:“6,9,10”,我从图层中获得并将其存储在变量中
var warning_day_1 = weather_warning_layer_data.features[0].properties.day_1
so basically the input is "day_1":"6,9,10" which I have stored in the array as所以基本上输入是“day_1”:“6,9,10” ,我将其存储在数组中
[{"warning":"6"},{"warning":"9"},{"warning":"10"}]
and corresponding output should be Dust Storm, Heat Wave, Hot Day和相应的 output 应该是沙尘暴,热浪,炎热的一天
Dust Storm, Heat Wave, Hot Day
or if the input was "day_1":"2,5" then output should have been Heavy Rain, Hailstorm或者如果输入是"day_1":"2,5"那么 output 应该是暴雨,冰雹
or if the input was "day_1":"1" then output should have been No Warning或者如果输入是"day_1":"1"那么 output 应该没有警告
After reading the data of the string and creating its array, I have to compare it with another array and extract the key values (display) corresponding to the key values (warning) in the 1st array.在读取字符串的数据并创建其数组后,我必须将其与另一个数组进行比较,并提取与第一个数组中的键值(警告)对应的键值(显示)。
var warning_data_split = warning_day_1.split(/[ ,]+/);
var warning_data_from_api_array = new Array;
warning_data_from_api_array.push(warning_data_split);
for (var i = 0; i < warning_data_from_api_array.length; i++) {
var item_in_array_to_compare = warning_data_from_api_array[i];
if(warning_data_from_api_array[item_in_array_to_compare.warning_data_from_api_array])
{warning_data_from_api_array[item_in_array_to_compare.warning_data_from_api_array].push(item_in_array_to_compare);}
else {
warning_data_from_api_array[item_in_array_to_compare.warning_data_from_api_array] = [item_in_array_to_compare];}}
let final_array_to_compare = item_in_array_to_compare
final_array_to_compare = final_array_to_compare.map(x => ({warning: x}));
/// this is the first array ////////////
The values in this array are not static in length, as it keeps on changing like, sometimes the array has value [1] or [1,2], [2,5,8], [4,7,12], etc so I have to extract the corresponding values of display from the lookup array given below该数组中的值的长度不是 static,因为它不断变化,有时数组的值是 [1] 或 [1,2]、[2,5,8]、[4,7,12] 等所以我必须从下面给出的查找数组中提取相应的显示值
var warning_code_meaning_list = [
{ warning:"1", display:"No Warning"},
{ warning:"2", display:"Heavy Rain"},
{ warning:"3", display:"Heavy Snow"},
{ warning:"4", display:"Thunderstorm & Lightning, Squall etc"},
{ warning:"5", display:"Hailstorm"},
{ warning:"6", display:"Dust Storm"},
{ warning:"7", display:"Dust Raising Winds"},
{ warning:"8", display:"Strong Surface Winds"},
{ warning:"9", display:"Heat Wave"},
{ warning:"10", display:"Hot Day"},
{ warning:"11", display:"Warm Night"},
{ warning:"12", display:"Cold Wave"},
{ warning:"13", display:"Cold Day"},
{ warning:"14", display:"Ground Frost"},
{ warning:"15", display:"Fog"}
]
The data which I am getting in warning_day_1 (in the very first line of the code) is a string (this couldn't be saved as float/integer in the database column because sometimes there are more than 1 warning for a specific place, so I have stored this as a text in the database) Which I'm converting to an array after reading it from the API我在warning_day_1中获得的数据(在代码的第一行)是一个字符串(这不能在数据库列中保存为浮点数/整数,因为有时特定位置有多个警告,所以我已将其作为文本存储在数据库中)从 API 读取后,我将其转换为数组
Now this string, which I am fetching from API has variable data, Some time single digit like: 1 Sometime multiple: 1,2,3现在,我从 API 获取的这个字符串具有可变数据,有时是单个数字,例如:1 有时是倍数:1,2,3
And each of the integer present in this array corresponds to the specific text shown in the next array like if the warning is 2 it means the heavy rainfall, but if the string (later converted to an array, with “warning” as a key) has 2,5 as value, it means: heavy rainfall & Hailstorm并且该数组中出现的每个 integer 对应于下一个数组中显示的特定文本,如警告为 2 表示暴雨,但如果字符串(稍后转换为数组,以“警告”为键)有 2,5 作为值,这意味着:强降雨和冰雹
I want that the values which come up in array 1 (the dynamic one) got match with the 2nd array ( a sort of lookup array) and fetch its display value as output.我希望数组 1(动态数组)中出现的值与第二个数组(一种查找数组)匹配,并将其显示值获取为 output。
How to do so?怎么做?
You could use an object to map your warnings to messages.您可以使用 object 到 map 对消息发出警告。
Try this:尝试这个:
const data = {"type":"FeatureCollection","features":[{"type":"Feature","id":"weather_warning_day_1.fid--418ec0da_178b69d5dfc_-715c","geometry":null,"properties":{"issue_date":"2021-04-09","updated_at":"2021-04-09T09:26:33+05:30","utc_time":0,"state_name":"Odisha","state_id":21,"district_name":"MAYURBHANJ","district_id":232,"api_district_name":"MAYURBHANJ","day_1":"6,9,10","day1_color":3}}],"totalFeatures":"unknown","numberReturned":1,"timeStamp":"2021-04-09T15:38:19.536Z","crs":null}
var warning_code_meaning_list = {
"1":"No Warning",
"2":"Heavy Rain",
"3":"Heavy Snow",
"4":"Thunderstorm & Lightning, Squall etc",
"5":"Hailstorm",
"6":"Dust Storm",
"7":"Dust Raising Winds",
"8":"Strong Surface Winds",
"9":"Heat Wave",
"10":"Hot Day",
"11":"Warm Night",
"12":"Cold Wave",
"13":"Cold Day",
"14":"Ground Frost",
"15":"Fog",
};
results = data["features"].map(feature => {
return feature.properties.day_1.split(',').map(code => {
return warning_code_meaning_list[code];
});
});
That gives you an array of arrays of the displays:这为您提供了一系列 arrays 显示器:
[ [ 'Dust Storm', 'Heat Wave', 'Hot Day' ] ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.