0

The aim of this function is to pass in: [{lang: 'English', proficiency: 'Elementary Proficiency'}, {lang: 'Spanish', proficiency: 'Full Professional Proficiency'}].

And have the function print out: [{level: 1, lang: 'English', proficiency: 'Elementary Proficiency'}, { level:3, lang: 'Spanish', proficiency: 'Full Professional Proficiency'}].

However, I'm getting: [{level:3, lang: 'Spanish', proficiency: 'Full Professional Proficiency}, {level:3, lang: 'Spanish', proficiency: 'Full Professional Proficiency}]. ANY IDEAS? Thank you.

lngSort(exObj) {
      let userprof = []
      let lng = []

      const proficiencies = [
        { level: 1, name: 'Elementary Proficiency' },
        { level: 2, name: 'Limited Working Proficiency' },
        { level: 3, name: 'Full Professional Proficiency' },
        { level: 4, name: 'Native Bilingual Proficiency' }
      ]

      for(let v in exObj) {
        userprof.push(exObj[v].proficiency);
        lng.push(exObj[v].lang);
      }

      let findmax = (proficiencies, userprof) => {
          const arr = [];
          const ob = {};

          let found = false;
          for (const prof of proficiencies) {
            for (const user of userprof) {
              if (found) found = false
              for (const l of lng) {
                if (prof.name === user) {
                  ob.level = qual.level
                  ob.proficiency = qual.name
                  ob.lang = l
                  arr.push(ob);
                  found = true
                  break
                }
              }
            }
          }
          console.log("QUAL: " + JSON.stringify(arr))
      }
      findmax(proficiencies, userqual);
    }
1
  • 2
    Just an input and output is not enough. Please provide an input and output as well as explaining what you want your code to do in a higher-level sense. Commented Nov 20, 2020 at 15:10

4 Answers 4

1

Looks like using map is simpler solution for this question.

const proficiencies = [
  { level: 1, name: 'Elementary Proficiency' },
  { level: 2, name: 'Limited Working Proficiency' },
  { level: 3, name: 'Full Professional Proficiency' },
  { level: 4, name: 'Native Bilingual Proficiency' }
];

const items = [
  { lang: 'English', proficiency: 'Elementary Proficiency' },
  { lang: 'Spanish', proficiency: 'Full Professional Proficiency' }
];


const res = items.map((item) => {
  const proficiency = proficiencies.find(i => i.name === item.proficiency);
  if (proficiency) {
    item.level = proficiency.level;
  }
  return item;
});

console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

1

You're only breaking the deepest level of for. You need to break the others too. Currently, the next loop doesn't break, starts over, and the first instruction in this loop is if (found) found = false, which resets your boolean, so the job is done again.

for (const prof of proficiencies) {
    for (const user of userprof) {
        if (found) found = false
        for (const l of lng) {
            if (prof.name === user) {
                ob.level = qual.level
                ob.proficiency = qual.name
                ob.lang = l
                arr.push(ob);
                found = true
                break
            }
        }
        if (found) break;
    }
    if (found) break;
}

Comments

0

If you restructure your proficiencies array as an object this becomes very simple with a single map().

const input = [{lang: 'English', proficiency: 'Elementary Proficiency'}, {lang: 'Spanish', proficiency: 'Full Professional Proficiency'}];

const proficiencies = {
        'Elementary Proficiency': 1,
        'Limited Working Proficiency': 2,
        'Full Professional Proficiency': 3, 
        'Native Bilingual Proficiency': 4
      }

const out = input.map(o => ({level: proficiencies[o.proficiency],...o}));

console.log(out);

Comments

0

You could take a slightly different approach to your problem which you might find easier to implement. Rather than using multiple loops, you can create a Map (which behaves much like an object, it keeps key-value pairs that can later be accessed using .get(key)). The map can be built from the proficiency array using the Map constructor. The map constructor takes an array of [[key1, value1], [key2, value2], ...] pairs which you can build using the .map() method on your array. Once you have built the map it will have the following structure:

"Elementary Proficiency" => 1
"Limited Working Proficiency" => 2
"Full Professional Proficiency" => 3
"Native Bilingual Proficiency" => 4

Where the numbers are the values which can be obtained from the map by using .get(proficiencyName). Once you have this structure, all you need to do is transform all the objects in your original array to have a level property. To do this, I have used .map() again, which will build a new object using the properties of each object currently present in your array (this is done using the spread syntax ...), and will add an additional level property which represents the level that is obtained by looking up the object's proficiency in the Map we made above:

const arr = [{lang: 'English', proficiency: 'Elementary Proficiency'}, {lang: 'Spanish', proficiency: 'Full Professional Proficiency'}];
const proficiencies = [ { level: 1, name: 'Elementary Proficiency' }, { level: 2, name: 'Limited Working Proficiency' }, { level: 3, name: 'Full Professional Proficiency' }, { level: 4, name: 'Native Bilingual Proficiency' } ];

const levelLookUp = new Map(proficiencies.map(o => [o.name, o.level]));
const result = arr.map(obj => ({level: levelLookUp.get(obj.proficiency), ...obj}));

console.log(result);

Building a Map first will be more performant as your array input sizes grow large, as it will only require you to loop over your proficiencies array and your input array once independently, rather than using nested loops which can have performance implications.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.