Javascript: 如何在一个查找数组中匹配多个变量?
Javascript: how to match multiple variables in one lookup array?
下面的实例
假设我想将城市缩写与其城市名称相匹配。最简单的方法是什么?我尝试使用查找,但不确定如何将变量数组与查找数组匹配。
所以,我想得到结果:Los Angeles
、San Francisco
input1 = "LA";
input2 = null;
input3 = "SF";
result = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
}[input1, input2, input3];
document.write(result);
console.log(result);
如果我以你为例:
您可以使用 const cityKey = Object.keys(result);
,只获取 object
中的密钥。 (这个的输出,是一个 Array
类型)
第二步是filter
这个数组(cityKey
)。如果一次迭代符合您的条件,您可以 return true
或 return false
请检查此 fiddle 以获取更多信息:LIVE DEMO
非常简单的方法:
1) 将您的输入排列成数组
2) 将你的数组与你的 table/dictionary/map/lookup
匹配
[input1, input2, input3].forEach(cityCode => console.log(result[cityCode]));
您可以使用 map
。如果您想将任何空值保留为未定义:
const input = ["LA", null, "SF"];
const abbreviations = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
};
const result = input.map(e => abbreviations[e]);
console.log(result);
或过滤器null/false/undefined:
const input = ["LA", null, "SF"];
const abbreviations = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
};
const result = input.map(e => abbreviations[e]).filter(e => e);
console.log(result);
请注意,您的 result
查找对象的变量名称与结果数组冲突,因此我稍微调整了您的名称。
有很多方法可以做到这一点(map / forEach / reduce / while loop & shift :)
)等。这里有几个例子:
注意:我将 undefined
替换为 N/A
以及在 reduceReplace
函数中跳过任何 falsy等等
var objMap = { "LA": "Los Angles", "NY": "New York", "SF": "San Francisco"}
var match = ['LA', undefined, 'SF']
const mapReplace = (arr) => arr.map(x => !!x ? objMap[x] : 'N/A')
const reduceReplace = (arr) => arr.reduce((r, c) => { !!c ? r.push(objMap[c]) : r; return r }, [])
const shiftReplace = (arr) => {
var result = []
while(arr.length > 0) {
result.push(objMap[arr.shift()])
}
return result;
}
console.log('map: ', mapReplace(match)) // This will just replace the strings
console.log('reduce: ', reduceReplace(match)) // This will also skip any undefined/null etc
console.log('shift: ', shiftReplace(match)) // Just for fun
这里的 reduce 方法只是有一个函数来去除虚假值,如果你想在数组中包含 undefined
则根本不需要它,因为那时 map
会遍历每个元素和 return 相同大小的数组结果。
显然,您也可以使用 forEach
和其他方法一次遍历数组的一个元素。
您可以 filter
keys
然后 reduce
main object
基于它。
input1 = "LA";
input2 = null;
input3 = "SF";
var input=[input1,input2,input3];
var data = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
}
var filtered=Object.keys(data)
.filter(key => input.includes(key))
.reduce((obj, key) => {
obj[key]=data[key];
return obj;
}, {});
console.log(filtered);
console.log(Object.values(filtered));
下面的实例
假设我想将城市缩写与其城市名称相匹配。最简单的方法是什么?我尝试使用查找,但不确定如何将变量数组与查找数组匹配。
所以,我想得到结果:Los Angeles
、San Francisco
input1 = "LA";
input2 = null;
input3 = "SF";
result = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
}[input1, input2, input3];
document.write(result);
console.log(result);
如果我以你为例:
您可以使用 const cityKey = Object.keys(result);
,只获取 object
中的密钥。 (这个的输出,是一个 Array
类型)
第二步是filter
这个数组(cityKey
)。如果一次迭代符合您的条件,您可以 return true
或 return false
请检查此 fiddle 以获取更多信息:LIVE DEMO
非常简单的方法:
1) 将您的输入排列成数组
2) 将你的数组与你的 table/dictionary/map/lookup
[input1, input2, input3].forEach(cityCode => console.log(result[cityCode]));
您可以使用 map
。如果您想将任何空值保留为未定义:
const input = ["LA", null, "SF"];
const abbreviations = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
};
const result = input.map(e => abbreviations[e]);
console.log(result);
或过滤器null/false/undefined:
const input = ["LA", null, "SF"];
const abbreviations = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
};
const result = input.map(e => abbreviations[e]).filter(e => e);
console.log(result);
请注意,您的 result
查找对象的变量名称与结果数组冲突,因此我稍微调整了您的名称。
有很多方法可以做到这一点(map / forEach / reduce / while loop & shift :)
)等。这里有几个例子:
注意:我将 undefined
替换为 N/A
以及在 reduceReplace
函数中跳过任何 falsy等等
var objMap = { "LA": "Los Angles", "NY": "New York", "SF": "San Francisco"}
var match = ['LA', undefined, 'SF']
const mapReplace = (arr) => arr.map(x => !!x ? objMap[x] : 'N/A')
const reduceReplace = (arr) => arr.reduce((r, c) => { !!c ? r.push(objMap[c]) : r; return r }, [])
const shiftReplace = (arr) => {
var result = []
while(arr.length > 0) {
result.push(objMap[arr.shift()])
}
return result;
}
console.log('map: ', mapReplace(match)) // This will just replace the strings
console.log('reduce: ', reduceReplace(match)) // This will also skip any undefined/null etc
console.log('shift: ', shiftReplace(match)) // Just for fun
这里的 reduce 方法只是有一个函数来去除虚假值,如果你想在数组中包含 undefined
则根本不需要它,因为那时 map
会遍历每个元素和 return 相同大小的数组结果。
显然,您也可以使用 forEach
和其他方法一次遍历数组的一个元素。
您可以 filter
keys
然后 reduce
main object
基于它。
input1 = "LA";
input2 = null;
input3 = "SF";
var input=[input1,input2,input3];
var data = {
"LA": "Los Angles",
"NY": "New York",
"SF": "San Francisco"
}
var filtered=Object.keys(data)
.filter(key => input.includes(key))
.reduce((obj, key) => {
obj[key]=data[key];
return obj;
}, {});
console.log(filtered);
console.log(Object.values(filtered));