我想获得一个基于我检查的值的键名列表。
这就是我所拥有的。
const data = {
"DE": "ARUN",
"HI": "ARUN",
"PR": "ARUN",
"TX": "ARUN",
"MA": "ARUN",
"MD": "CHARLES,BRANDON,DAVID,ARUN",
"IA": "ARUN",
"ME": "ARUN",
"ID": "ARUN",
"MI": "ARUN",
"UT": "ARUN",
"MN": "ARUN",
"MO": "ARUN",
"IL": "ARUN",
"IN": "ARUN",
"MS": "ARUN",
"MT": "ARUN",
"AK": "ARUN",
"AL": "ARUN",
"VA": "ARUN",
"AR": "ARUN",
"AS": "ARUN",
"VI": "ARUN",
"NC": "ARUN",
"ND": "ARUN",
"NE": "ARUN",
"RI": "ARUN",
"AZ": "ARUN",
"NH": "ARUN",
"NJ": "ARUN",
"VT": "ARUN",
"NM": "ARUN",
"FL": "ARUN",
"NV": "ARUN",
"WA": "ARUN",
"NY": "ARUN",
"SC": "ARUN",
"SD": "ARUN",
"WI": "ARUN",
"OH": "ARUN",
"GA": "ARUN",
"OK": "ARUN",
"CA": "ARUN",
"WV": "ARUN",
"WY": "ARUN",
"OR": "ARUN",
"KS": "ARUN",
"CM": "ARUN",
"CO": "ARUN",
"GU": "ARUN",
"KY": "ARUN",
"CT": "ARUN",
"PA": "ARUN",
"LA": "ARUN",
"TN": "ARUN",
"DC": "ARUN"
}
const name = 'BRANDON'
const newMappedData = Object.keys(data).map(key => {
return {
[key]: data[key].split(',')
}
})
const filteredData = newMappedData.filter(item => Object.values(item).includes(name))
console.log(filteredData)
在上面的示例中,当我们输入“BRANDON”时,结果应仅为
['MD']
,如果为“ARUN”,则应给我['DE', 'HI', 'PR',...]
请指教。
最佳答案
如果您使用includes
,甚至不需要split
。另外,您不需要使用map
来创建新的数组:
const data = {
"DE": "ARUN",
"HI": "ARUN",
"PR": "ARUN",
"TX": "ARUN",
"MA": "ARUN",
"MD": "CHARLES,BRANDON,DAVID,ARUN",
"IA": "ARUN",
"ME": "ARUN",
"ID": "ARUN",
"MI": "ARUN",
"UT": "ARUN",
"MN": "ARUN",
"MO": "ARUN",
"IL": "ARUN",
"IN": "ARUN",
"MS": "ARUN",
"MT": "ARUN",
"AK": "ARUN",
"AL": "ARUN",
"VA": "ARUN",
"AR": "ARUN",
"AS": "ARUN",
"VI": "ARUN",
"NC": "ARUN",
"ND": "ARUN",
"NE": "ARUN",
"RI": "ARUN",
"AZ": "ARUN",
"NH": "ARUN",
"NJ": "ARUN",
"VT": "ARUN",
"NM": "ARUN",
"FL": "ARUN",
"NV": "ARUN",
"WA": "ARUN",
"NY": "ARUN",
"SC": "ARUN",
"SD": "ARUN",
"WI": "ARUN",
"OH": "ARUN",
"GA": "ARUN",
"OK": "ARUN",
"CA": "ARUN",
"WV": "ARUN",
"WY": "ARUN",
"OR": "ARUN",
"KS": "ARUN",
"CM": "ARUN",
"CO": "ARUN",
"GU": "ARUN",
"KY": "ARUN",
"CT": "ARUN",
"PA": "ARUN",
"LA": "ARUN",
"TN": "ARUN",
"DC": "ARUN"
}
const name = 'BRANDON'
const filteredData = Object.keys(data).filter(key => data[key].includes(name))
console.log(filteredData)