Javascript: how to match multiple variables in one lookup array?

javascript string contains multiple values
javascript check if multiple variables are equal
javascript indexof multiple occurrence
excel match multiple criteria from different arrays
how to check multiple values in if condition in javascript
index match multiple criteria without array
javascript compare multiple values
javascript multiple comparisons

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

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);

You can use 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);

A Better Way to Perform Multiple Comparisons in JavaScript, A Better Way to Perform Multiple Comparisons in JavaScript indexOf() method to see if it exists in an array of values. It's much smarter for you to use this method than loop over every value and match it against your string. This approach would be helpful if we have to handle for multiple classifications of fruits. But for one check it’s a lot of typing and takes up a lot of space. Array Based. ECMAScript 5 introduced an indexOf method on Array.prototype . We can use this to create an array on the fly, then see whether the value matches one of the values in the

There are more than few ways to do this (map / forEach / reduce / while loop & shift :)) etc. Here are few examples:

Note: I am replacing the undefined with N/A as well as in the reduceReplace function skipping any falsy etc.

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

Complete Guide to INDEX & MATCH for Flexible Lookups in Excel, Lookup multiple criteria in rows or columns The first argument of INDEX is to give it an array. Like VLOOKUP, Match needs a lookup (target) value. to “​Sales”, the results are changed to SUM the values from the sales column. Try watching this video on www.youtube.com, or enable JavaScript if it is disabled in your  With MATCH, the easiest way to create an array formula is by using the & symbol, like so: = MATCH (lookup_value_1 & lookup_value_2, lookup_array_1 & lookup_array_2, match_type) It's very important to note that when you use an array formula like this one, you'll need to commit your formula using Ctrl+Shift+Enter rather than just pressing Enter. This will tell Excel that you're using an array formula rather than a standard formula.

You can filter the keys and then reduce the main object based on it.

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));

Lookup multiple criteria in rows or columns - Xelplus, The MATCH() function is used to find at what row number the lookup value is found. helper cells that combine the values in rows 20 and 21 into one row instead of two. This involves generating an array for the MATCH() function by pressing the Try watching this video on www.youtube.com, or enable JavaScript if it is  Like a VLOOKUP for multiple criteria, INDEX and MATCH were designed with the lookup of one value in mind, but you can expand it for multiple values with a few tricks. What remains the same in any lookup function is that there needs to be a unique value to look up.

If I take your example :

You can probably use const cityKey = Object.keys(result);, to get only the keys inside your object. (The output of this, is an Array type)

The second step is to filter this array (cityKey). And If one iteration match with your condition you can return true or return false

Please check this fiddle for your more information : LIVE DEMO

Excel formula: INDEX and MATCH with multiple criteria, To lookup values with INDEX and MATCH, using multiple criteria, you can use an array formula. In the example shown, the formula in H8 is: which is fed into the MATCH function as the lookup array, with a lookup value of 1: MATCH ( 1 , { 0 ; 0 ; 1 ; 0 ; 0 ; 0 ; 0 } ) At this point, the formula is a standard INDEX MATCH formula.

Pretty simple way:

1) Arrange your inputs as an array 2) Match your array against your table/dictionary/map/lookup

[input1, input2, input3].forEach(cityCode => console.log(result[cityCode]));

Excel tutorial: XLOOKUP with multiple lookup values, In this video, we'll set up XLOOKUP to return multiple values in a dynamic array, by providing a range of lookup values instead of a single lookup value. I'll skip the not found message, and I'll set match mode to -1 for exact match or next smallest. You must have JavaScript enabled to use this form. VLOOKUP function returns only the first match – “Smith”. Returning multiple matches and display them vertically. If you want to return more than one value, you have to use array formulas. Array formulas are designed to work with a series of cells, rather than a single cell. Enter the following formula into cell E2 and press Ctrl + Shift

Excel formula: INDEX and MATCH with multiple criteria, How to use INDEX and MATCH with multiple criteria. An array formula can be used to lookup values that meet multiple criteria based on INDEX and MATCH  How to lookup one value and return multiple values associated with it. This is an array function that needs to be entered with the keyboard combination CTRL+SHIFT+ENTER, which essentially enters

Excel Lookup Multiple Criteria Index Match, How to do an Excel lookup with multiple criteria in columns, with the This video shows how the INDEX and MATCH functions work together, with one criterion. Only the 8th row in our list of items has a 1, because both values are we will use an array-entered INDEX and MATCH formula to do all the  Enter it as an array formula. After writing formula hit CTRL+SHIFT+ENTER key to make it an array formula. Example of lookup for multiple results. I have this student data in range A2:E14. In cell G1, I have a drop-down of region values e.g. Central, East, North, South, and West. Now I want, whichever region I have in G1, a list of all students

Indexed collections, An array is an ordered set of values that you refer to with a name and an index. JavaScript does not have an explicit array data type. However searches the array for searchElement and returns the index of the first match.

Comments
  • do you not want to loop the inputs and access your obj like let city = myObj[currentInput] ?
  • surprised why this answer was down-voted?.More surprised when it was deleted.+1
  • @NullPointer It looks like someone just went on a downvoting spree in this thread. I think this is a case where OP asked a question, everyone made some reasonable best practice assumptions (e.g. more cities will be in the object, result not an object, no input1, input2 etc) but OP really wants a very literal interpretation that ignores all of these assumptions. I'll generally delete my post if it's not helpful or I wasn't clear enough on the question. Thanks for the +1 though.
  • I'm not the one downvoting. Thanks for this beautiful solution.
  • If you have a better solution, answer this post and downvote, but otherwise if you have no solution why downvote ?