How to create key value pair using two arrays in JavaScript?

13.6k views Asked by At

I have two arrays, keys and commonkeys.

I want to create a key-value pair using these two arrays and the output should be like langKeys.

How to do that?

This is array one:

var keys=['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT']

This is array two:

var commonKeys=['en-*','es-*', 'pt-*','fr-*','de-*','ja-*','it-*', '*']

This is the output I need:

var langKeys = {
    'en-*': 'en_US',
    'es-*': 'es_ES',
    'pt-*': 'pt_PT',
    'fr-*': 'fr_FR',
    'de-*': 'de_DE',
    'ja-*': 'ja_JP',
    'it-*': 'it_IT',
    '*': 'en_US'
};
7

There are 7 answers

0
mike510a On

Use a for loop to iterate through both of the arrays, and assign one to the other using array[i] where i is a variable representing the index position of the value.

var keys = ['en_US', 'es_ES', 'pt_PT', 'fr_FR', 'de_DE', 'ja_JP', 'it_IT'];


var commonKeys = ['en-*', 'es-*', 'pt-*', 'fr-*', 'de-*', 'ja-*', 'it-*', '*'];

var langKeys = {};
for (var i = 0; i < keys.length; i++) {
  var commonkey = commonKeys[i];
  langKeys[commonkey] = keys[i];
}
console.log(JSON.stringify(langKeys));

2
Manoj Lodhi On

Try this may be it helps.

  var langKeys = {};
  var keys=['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT']
  var commonKeys=['en-*','es-*', 'pt-*','fr-*','de-*','ja-*','it-*', '*']
  function createArray(element, index, array) {
     langKeys[element]= keys[index];
     if(!keys[index]){
      langKeys[element]= keys[index-(commonKeys.length-1)];
     }
  }

  commonKeys.forEach(createArray);
  console.info(langKeys);
0
Rohan Orton On

JavaScript is a very versatile language, so it is possible to do what you want in a number of ways. You could use a basic loop to iterate through the arrays, like this:

var keys=['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT']
var commonKeys=['en-*','es-*', 'pt-*','fr-*','de-*','ja-*','it-*', '*']

var i;
var currentKey;
var currentVal;

var result = {}


for (i = 0; i < keys.length; i++) {
    currentKey = commonKeys[i];
    currentVal = keys[i];
    result[currentKey] = currentVal;    
}

This example will work in all browsers.

0
Alexis On

You can use map() function on one array and create your objects

var keys=['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT'];
var commonKeys=['en-*','es-*', 'pt-*','fr-*','de-*','ja-*','it-*', '*'];

var output = keys.map(function(obj,index){
  var myobj = {};
  myobj[commonKeys[index]] = obj;
  return myobj;
});

console.log(output);

0
SimonC On

What you want to achieve is to create an object from two arrays. The first array contains the values and the second array contains the properties names of the object.

As in javascript you can create new properties with variales, e.g.

objectName[expression] = value; // x = "age"; person[x] = 18,

you can simply do this:

var keys=['en_US','es_ES', 'pt_PT','fr_FR','de_DE','ja_JP','it_IT'];
var commonKeys=['en-*','es-*', 'pt-*','fr-*','de-*','ja-*','it-*', '*'];

var langKeys = {};

var i;
for (i=0; i < keys.length; i++) {
    langKeys[commonKeys[i]] = keys[i];
}

EDIT

This will work only if both arrays have the same size (actually if keys is smaller or same size than commonKeys).

For the last element of langKeys in your example, you will have to add it manually after the loop.

What you wanted to achieve was maybe something more complicated, but then there is missing information in your question.

0
Remmar00 On

ES6 update:

let commonKeys = ['en-*', 'es-*', 'pt-*', 'fr-*', 'de-*', 'ja-*', 'it-*', '*'];
let keys = ['en_US', 'es_ES', 'pt_PT', 'fr_FR', 'de_DE', 'ja_JP', 'it_IT', 'en_US'];

let zipArrays = (keysArray, valuesArray) => Object.fromEntries(keysArray.map((value, index) => [value, valuesArray[index]]));

let langKeys = zipArrays(commonKeys, keys);
console.log(langKeys);

// let langKeys = Object.fromEntries(commonKeys.map((val, ind) => [val, keys[ind]]));

0
Roberto Garcia On

    let keys = ['en_US', 'es_ES', 'pt_PT', 'fr_FR', 'de_DE', 'ja_JP', 'it_IT'];
let commonKeys = ['en-*', 'es-*', 'pt-*', 'fr-*', 'de-*', 'ja-*', 'it-*', '*'];

// declaration of empty object where we'll store the key:value
let result = {}; 

// iteration over first array to pick up the index number 
for (let i in keys) {
    // for educational purposes, showing the number stored in i (index)
    console.log(`index number: ${i}`);

    // filling the object with every element indicated by the index
    // objects works in the basis of key:value so first position of the index(i)
    // will be filled with the first position of the first array (keys) and the second array (commonKeys)  and so on.

    result[keys[i]] = commonKeys[i];
    // keep in mind that for in will iterate through the whole array length
}
console.log(result);