在JavaScript中,如何根据一个数组中的值与另一个数组的相应值匹配后提取数据



这是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}

我要提取的数据是可变的:;第1天":"6,9,10〃;我从层中得到,并将其作为存储在变量中

var warning_day_1 = weather_warning_layer_data.features[0].properties.day_1

所以基本上输入是"strong";第1天":"6,9,10〃我已将其作为存储在阵列中

[{"warning":"6"},{"warning":"9"},{"warning":"10"}]

相应的输出应为沙尘暴、热浪、热天

Dust Storm, Heat Wave, Hot Day

或者如果输入是"strong";第1天":"2,5〃那么输出应该是大雨,冰雹

或者如果输入是"strong";第1天":"1〃则输出应为无警告

在读取字符串的数据并创建其数组后,我必须将其与另一个数组进行比较,并提取与第一个数组中的键值(警告(相对应的键值(显示(。

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 ////////////

此数组中的值在长度上不是静态的,因为它不断变化,例如,有时数组具有值[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"}
]

我在warning_day_1中得到的数据(在代码的第一行(是一个字符串(不能在数据库列中保存为float/integer,因为有时特定位置有不止一个警告,所以我将其作为文本存储在数据库中(从API 中读取后,我将其转换为数组

现在我从API获取的这个字符串有变量数据,有些时候是个位数,比如:1有时倍数:1,2,3

这个数组中的每个整数都对应于下一个数组中显示的特定文本,就像如果警告是2,但如果字符串(后来转换为数组,以"warning"为键(的值为2,5,则表示:强降雨&冰雹

我希望数组1(动态数组(中出现的值与第二个数组(一种查找数组(匹配,并获取其显示值作为输出。

如何做到这一点?

您可以使用一个对象将警告映射到消息。

试试这个:

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];
});
});

这给了你们一个显示器阵列阵列:

[ [ 'Dust Storm', 'Heat Wave', 'Hot Day' ] ]

相关内容

  • 没有找到相关文章

最新更新