logoologist
logoologist

Reputation: 205

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

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

Upvotes: 0

Views: 1175

Answers (5)

Akrion
Akrion

Reputation: 18525

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

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.

Obviously you can also use forEach and other methods to go over the array one element at a time.

Upvotes: 1

NullPointer
NullPointer

Reputation: 7368

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

Upvotes: 0

ggorlen
ggorlen

Reputation: 57185

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

Or filter 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.

Upvotes: 1

Luiz Chagas Jr
Luiz Chagas Jr

Reputation: 149

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

Upvotes: 0

KolaCaine
KolaCaine

Reputation: 2187

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

Upvotes: 0

Related Questions