在 json 和 return 主键中搜索值
search for value in json and return the main key
我有一个 json 结构像这样
{
"india": {
"north_states": ["TN", "AP", "MP", "AB"],
"south_states": ["PB", "UP", "RJ", "HR"]
},
"us": {
"north_states": ["AC", "AD", "AE", "AF"],
"south_states": ["BA", "BB", "BC", "BD"]
},
}
现在如果我搜索“UP”,我必须得到 south_states
和 india
作为回复。我如何在 JavaScript 中做到这一点?
例如:
我需要根据搜索中给出的输入获取国家/地区名称。例如,如果我得到的输入值为 'UP',我需要搜索 UP 所在的 json 并获取它的键 (south_states),我还需要将国家/地区名称设为印度。结果应采用州首字母缩写名称的输入,并将键作为国家名称和 state_location(南或北)
我会以这种方式迭代:
代码可能需要一些调整,但它有效。
这使用递归来迭代 JSON
以找到匹配的 objects/array。如果JSON的结构是固定的,那就更简单了。
const countries = { "india":{"north_states":["TN","AP","MP","AB"],"south_states":["PB","UP","RJ","HR"]}, "us":{"north_states":["AC","AD","AE","AF"],"south_states":["BA","BB","BC","BD"]}, };
function findItems(data, value, pkey){
let results = [];
for(let key of Object.keys(data)){
let newData = data[key];
if(typeof(newData) == 'string'){
if(newData == value){
results.push(pkey);
}
} else {
results = findItems(newData, value, key).concat(results);
}
}
if(results.length > 0 && pkey && results.indexOf(pkey) == -1){
results.push(pkey);
}
return results;
}
let search = findItems(countries, 'UP');
console.info(search);
对于固定结构,只应找到结果:
const countries = { "india":{"north_states":["TN","AP","MP","AB"],"south_states":["PB","UP","RJ","HR"]}, "us":{"north_states":["AC","AD","AE","AF"],"south_states":["BA","BB","BC","BD"]}, };
function findItems(data, searchTerm){
let results = [];
for(let country of Object.keys(data)){
let stateGroups = data[country];
for(let stateGroupName of Object.keys(stateGroups)){
let stateGroup = stateGroups[stateGroupName];
if(stateGroup.indexOf(searchTerm)!=-1){
return [country, stateGroupName];
}
}
}
}
let search = findItems(countries, 'UP');
console.info(search);
Parse the JSON 转换为可以循环的 JS 对象。
Loop over the main object properties, and then loop over each of those object's properties, and if any of those arrays includes
你要匹配的值,return主对象的key,内层对象的key。
const json = '{"india":{"north_states":["TN","AP","MP","AB"],"south_states":["PB","UP","RJ","HR"]},"us":{"north_states":["AC","AD","AE","AF"],"south_states":["BA","BB","BC","BD"]}}';
const data = JSON.parse(json);
function check(data, val) {
for (let key in data) {
for (let innerkey in data[key]) {
if (data[key][innerkey].includes(val)) {
return [key, innerkey];
}
}
}
return 'No match';
}
console.log(check(data, 'UP'));
console.log(check(data, 'BO'));
console.log(check(data, 'AF'));
我有一个 json 结构像这样
{
"india": {
"north_states": ["TN", "AP", "MP", "AB"],
"south_states": ["PB", "UP", "RJ", "HR"]
},
"us": {
"north_states": ["AC", "AD", "AE", "AF"],
"south_states": ["BA", "BB", "BC", "BD"]
},
}
现在如果我搜索“UP”,我必须得到 south_states
和 india
作为回复。我如何在 JavaScript 中做到这一点?
例如:
我需要根据搜索中给出的输入获取国家/地区名称。例如,如果我得到的输入值为 'UP',我需要搜索 UP 所在的 json 并获取它的键 (south_states),我还需要将国家/地区名称设为印度。结果应采用州首字母缩写名称的输入,并将键作为国家名称和 state_location(南或北)
我会以这种方式迭代:
代码可能需要一些调整,但它有效。
这使用递归来迭代 JSON
以找到匹配的 objects/array。如果JSON的结构是固定的,那就更简单了。
const countries = { "india":{"north_states":["TN","AP","MP","AB"],"south_states":["PB","UP","RJ","HR"]}, "us":{"north_states":["AC","AD","AE","AF"],"south_states":["BA","BB","BC","BD"]}, };
function findItems(data, value, pkey){
let results = [];
for(let key of Object.keys(data)){
let newData = data[key];
if(typeof(newData) == 'string'){
if(newData == value){
results.push(pkey);
}
} else {
results = findItems(newData, value, key).concat(results);
}
}
if(results.length > 0 && pkey && results.indexOf(pkey) == -1){
results.push(pkey);
}
return results;
}
let search = findItems(countries, 'UP');
console.info(search);
对于固定结构,只应找到结果:
const countries = { "india":{"north_states":["TN","AP","MP","AB"],"south_states":["PB","UP","RJ","HR"]}, "us":{"north_states":["AC","AD","AE","AF"],"south_states":["BA","BB","BC","BD"]}, };
function findItems(data, searchTerm){
let results = [];
for(let country of Object.keys(data)){
let stateGroups = data[country];
for(let stateGroupName of Object.keys(stateGroups)){
let stateGroup = stateGroups[stateGroupName];
if(stateGroup.indexOf(searchTerm)!=-1){
return [country, stateGroupName];
}
}
}
}
let search = findItems(countries, 'UP');
console.info(search);
Parse the JSON 转换为可以循环的 JS 对象。
Loop over the main object properties, and then loop over each of those object's properties, and if any of those arrays
includes
你要匹配的值,return主对象的key,内层对象的key。
const json = '{"india":{"north_states":["TN","AP","MP","AB"],"south_states":["PB","UP","RJ","HR"]},"us":{"north_states":["AC","AD","AE","AF"],"south_states":["BA","BB","BC","BD"]}}';
const data = JSON.parse(json);
function check(data, val) {
for (let key in data) {
for (let innerkey in data[key]) {
if (data[key][innerkey].includes(val)) {
return [key, innerkey];
}
}
}
return 'No match';
}
console.log(check(data, 'UP'));
console.log(check(data, 'BO'));
console.log(check(data, 'AF'));