[英]Javascript: how to match multiple variables in one lookup array?
live example below 下面的直播示例
Let's say I want to match city abbreviations with their city names. 假设我想将城市缩写与城市名称相匹配。 What is the simplest way to do that? 最简单的方法是什么? I tried using a lookup, but not sure how to match an array of variables with the lookup array. 我尝试使用查找,但是不确定如何将变量数组与查找数组匹配。
So, I want to get the results: Los Angeles
, San Francisco
所以,我想得到结果: 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);
There are more than few ways to do this ( map / forEach / reduce / while loop & shift :)
) etc. Here are few examples: 有很多方法可以做到这一点( map / forEach / reduce / while loop & shift :)
)等等。这里有几个例子:
Note : I am replacing the undefined
with N/A
as well as in the reduceReplace
function skipping any falsy etc. 注意 :我用N/A
代替undefined
,以及在reduceReplace
函数中跳过任何虚假的等等。
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
The reduce approach here is simply to have one function to get rid of the falsy values it is not needed at all if you want to have the undefined
in the array since then map
would go though each element and return the same size array result. 这里的reduce方法只是具有一个函数来消除伪造的值,如果您想在数组中具有undefined
,则根本不需要它,因为map
会遍历每个元素并返回相同大小的数组结果。
Obviously you can also use forEach
and other methods to go over the array one element at a time. 显然,您也可以使用forEach
和其他方法一次遍历数组一个元素。
You can use map
. 您可以使用map
。 If you want to keep any nulls as undefined: 如果要将任何空值保留为未定义:
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);
Or filter null/false/undefined: 或过滤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);
Note that your result
variable name for the lookup object conflicts with the result array, so I adjusted your names a bit. 请注意,您的查找对象的result
变量名称与结果数组冲突,因此我对您的名称进行了一些调整。
You can filter
the keys
and then reduce
the main object
based on it. 您可以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));
If I take your example : 以我为例:
You can probably use const cityKey = Object.keys(result);
您可能可以使用const cityKey = Object.keys(result);
, to get only the keys inside your object
. ,仅获取object
内部的键。 (The output of this, is an Array
type) (此输出为Array
类型)
The second step is to filter
this array ( cityKey
). 第二步是filter
该数组( cityKey
)。 And If one iteration match with your condition you can return true
or return false
而且,如果一次迭代符合您的条件,则可以return true
或return false
Please check this fiddle for your more information : LIVE DEMO 请检查此小提琴以获取更多信息: 现场演示
Pretty simple way: 很简单的方法:
1) Arrange your inputs as an array 1)将输入排列为数组
2) Match your array against your table/dictionary/map/lookup 2)将数组与表/字典/地图/查找匹配
[input1, input2, input3].forEach(cityCode => console.log(result[cityCode]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.