简体   繁体   English

Javascript:如何在一个查找数组中匹配多个变量?

[英]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 AngelesSan 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 truereturn 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.

相关问题 MongoDB 聚合 - 如何将 $match 和 $lookup 结果合并到一个数组中 - MongoDB Aggregation - How to Merge $match and $lookup results into one array 如何减少Javascript中多个变量上的对象数组? - How to reduce an array of objects on multiple variables in Javascript? 如何在 Javascript 中对数组应用查找? - How to apply lookup to array in Javascript? 如何获取JavaScript以匹配包含变量并跨越多行的正则表达式? - How to get JavaScript to match a regex containing variables and spanning multiple lines? javascript / ajax 一键将多个变量按id设置为数组 - javascript / ajax with one click set multiple variables by ids as an array 如何将一个数组与另一个数组匹配,但将匹配的元素保存在JavaScript中 - How to match one array to another array but saving matching elements in javascript 如果使用JavaScript,如何检查数组中的多个元素? - How to check multiple elements in array with one if with JavaScript? 如何在javascript中将多个对象放在一个数组中? - How to put multiple objects in one array in javascript? 如何在JavaScript中将一个变量拆分为多个变量? - how do I split from one variable into multiple variables in javascript? 如何使用Javascript中的一个函数为多个变量分配不同的值 - How to assign multiple variables different values using one function in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM