uiTeam324
uiTeam324

Reputation: 1245

Group case sensitive values using Javascript

Consider the scenario, where we have models ABC23x , ABC23X & abc23X. They refer to the same model. This list of model names are coming form api end points.

Now UI has 2 things to do:

I need some help to implement this feature. I tried to that using MAP and it's not working as expected.

let models = ['tc75X', 'TC75X', 'tc75x', 'TC75x', 'TC76x', 'TC77Y'];
let mappedModels = new Map(models.map(s => [s.toUpperCase(), s]));

console.log(mappedModels);

Here is the fiddle

I am using angular 6. Any help / suggestions would be very helpful.

Upvotes: 0

Views: 75

Answers (4)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

let models = ['tc75X', 'TC75X', 'tc75x', 'TC75x', 'TC76x', 'TC77Y'];

let mappedModels = models.reduce((acc, c) =>{
    let cUp = c.toUpperCase(); 
	acc[cUp] = acc[cUp] || [];
	acc[cUp].push(c);
	return acc
}, {});

for (let [key, value] of Object.entries(mappedModels)) {
  console.log(`${key}: ${value}`);
}

You can use reduce method

Upvotes: 0

junwen-k
junwen-k

Reputation: 3654

You can save your models as an object. Each unique key defines the uppercased model and its value is the array of model with different casing.

const models = ['tc75X', 'TC75X', 'tc75x', 'TC75x', 'TC76x', 'TC77Y'];

const map = models.reduce((res, model) => {
  const uppercasedModel = model.toUpperCase();
  if (!res[uppercasedModel]) {
    res[uppercasedModel] = [model];
  } else {
    res[uppercasedModel].push(model);
  }
  return res;
}, {})

console.log(map);

Upvotes: 0

VLAZ
VLAZ

Reputation: 29088

The issue you currently have is that each new key overwrites the old one. Here is what happens:

  1. You get 'tc75X' -> key is 'TC75X' -> gets added to the Map as 'TC75X' -> ['tc75X']
  2. You get 'TC75X' -> key is 'TC75X' -> gets added to the Map as 'TC75X' -> ['TC75X']

So you only get one value instead of two.

Instead you should group as you go along:

  1. If there is no key for the current model, then add it to the Map.
  2. Add the current value to the Map.

let models = ['tc75X', 'TC75X', 'tc75x', 'TC75x', 'TC76x', 'TC77Y'];

let mappedModels = models.reduce((map, modelName) => {
  const normalisedKey = modelName.toUpperCase();
  
  //1. Add entry if not present
  if (!map.has(normalisedKey)) {
    map.set(normalisedKey, [])
  }
  
  //2. Add to entry for this key
  map.get(normalisedKey).push(modelName);
  
  return map;
}, new Map())

for(let [key, value] of mappedModels) {
  console.log(key, "->", value);
}

Upvotes: 1

Tom Dezentje
Tom Dezentje

Reputation: 631

You could do something like this. First we create the key. Then we create a new array if the key is not yet in the map. Then we set the value with the new array.

const models = ['tc75X', 'TC75X', 'tc75x', 'TC75x', 'TC76x', 'TC77Y'];
const mappedModels = new Map();

for (const model of models) {
  const key = model.toUpperCase();
  let set = mappedModels.get(key);

  if (!set) {
    set = [];
  }

  set.push(model);
  mappedModels.set(key, set);
}

console.log(mappedModels);

Upvotes: 0

Related Questions