gpt4 book ai didi

javascript - 动态规划 : return all match data

转载 作者:行者123 更新时间:2023-11-30 15:07:05 25 4
gpt4 key购买 nike

我写了一个简单的动态程序,可以返回第一个匹配的数据。现在我想获取所有匹配的数据。我的数据:

let countryList = {
country: [{
name: "Bangladesh",
province: [{
name:"Dhaka",
city: [{
name:"Tangail",
lat: '11'
}, {
name:"Jamalpur",
lat: '12'
}]
}, {
name: "Khulna",
city: [{
name:"Jossore",
lat: '22'
}, {
name:"Tangail",
lat: '23'
}]
}, {
name: "Rajshahi",
city: [{
name:"Pabna",
lat: '33'
}, {
name:"Rangpur",
lat: '33'
}]
}]
},{
name: "India",
province: [{
name:"West Bengal",
city: [{
name:"Calcutta",
lat: '111'
}, {
name:"Tangail",
lat: '112'
}]
}, {
name: "Uttar Pradesh",
city: [{
name:"Agra",
lat: '122'
}, {
name:"Tajmahal",
lat: '123'
}]
}, {
name: "Rajasthan",
city: [{
name:"Kanpur",
lat: '131'
}, {
name:"Jaypur",
lat: '132'
}]
}]
}]
}

这里我有两个国家的省市。在这里用户可以搜索任何输入,如国家、省份或城市。例如,对于 Tangail,它应该返回所有 3 个结果。

预期输出:

[{
name:"Tangail",
lat: '11'
},{
name:"Tangail",
lat: '23'
}, {
name:"Tangail",
lat: '112'
}]

我试过如下所示:

function findName(obj, name) {  return obj.name.trim() === name;  }

function information( countryList, searchValue, current, state ) {

let nextState; // state is used to prevent call of undefined data
if ( state === "province" ) {
nextState = "city"; current = "country";
} else if( state === "city" ) {
nextState = "empty"; current = "province";
} else {
nextState = ""; current = "city";
}

// search matching data
let data = countryList.find( val => findName( val, searchValue ) );
// if not data found
if( typeof data === 'undefined' && nextState !== '' ) {
let len = countryList.length;
for( let x = 0; x < len; x++ ) { // now search all immediate child data
let status = {};
status.name = countryList[x].name;
status.lebel = current;
let info;
info = information(countryList[x][state], searchValue, current, nextState); // recursive call

if( typeof info !== 'undefined' ) { // if data found
return { state: status, data: info };
}
}

// return results;

} else {
return data;
}
}

let state = "province";
let current = "country";
let searchValue = "Tangail";
let abc = information( countryList.country, searchValue, current, state);
console.log("abc : ", abc);

并且输出始终是第一个匹配值。

有什么建议吗?提前致谢。

注意:我正在使用 javascript

最佳答案

首先使用 #reduce 转换成城市列表函数 - 然后使用该数组中的搜索字符串进行过滤 - 请参见下面的演示:

let countryList={country:[{name:"Bangladesh",province:[{name:"Dhaka",city:[{name:"Tangail",lat:"11"},{name:"Jamalpur",lat:"12"}]},{name:"Khulna",city:[{name:"Jossore",lat:"22"},{name:"Tangail",lat:"23"}]},{name:"Rajshahi",city:[{name:"Pabna",lat:"33"},{name:"Rangpur",lat:"33"}]}]},{name:"India",province:[{name:"West Bengal",city:[{name:"Calcutta",lat:"111"},{name:"Tangail",lat:"112"}]},{name:"Uttar Pradesh",city:[{name:"Agra",lat:"122"},{name:"Tajmahal",lat:"123"}]},{name:"Rajasthan",city:[{name:"Kanpur",lat:"131"},{name:"Jaypur",lat:"132"}]}]}]};

// convert into list of cities
var cities = countryList.country.reduce(function(p,c){
c.province.forEach(function(e) {
e.city.forEach(function(k){
p.push(k);
});
});
return p;
},[]);

// filter based on the search string
var result = cities.filter(function(e) {
return e.name == 'Tangail';
});
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

关于javascript - 动态规划 : return all match data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45590175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com