-1

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'
};
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
  • how is the last key value pair ( '*':'en_US' ) in langKeys created – Himanshu Tanwar Dec 20 '16 at 10:00
  • 7
    What have you tried? Do you know how to use loops and properties? Please show us your attempt even if it does not work. – Bergi Dec 20 '16 at 10:01
  • 1
    There's a pretty trivial, obvious, straight forward way involving a simple loop. There may be more elaborate, elegant or funky ways to do the same with different techniques. Where exactly lies your stumbling block? – deceze Dec 20 '16 at 10:02
  • What have you tried? Any code you have issues with, please add to the question so we can try replicate the problem. There is also no information on how you want to match the values. Will both arrays always be in the exact matching order? If so, just iterate through both arrays and concatonate each value from the same index. `'*':'en_US'` looks odd in the desired result, what are the rules for that? In essence I think at this point the question is to broad and missing a few bits of information. – Nope Dec 20 '16 at 10:03
  • It looks like you are just talking about stuffing values into an object? – le3th4x0rbot Dec 20 '16 at 10:03
  • 1
    There is no stumbling block. I want to know how to map these two arrays into key value pair. –  Dec 20 '16 at 10:04
  • So why do you need our help? – Liam Dec 20 '16 at 10:07
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam Dec 20 '16 at 10:10
  • @radiance88 edited my answer, it will give you same result as you need. CHEERS :) – Manoj Lodhi Dec 20 '16 at 10:20
  • Does this answer your question? [Creating a JavaScript Object from two arrays](https://stackoverflow.com/questions/39127989/creating-a-javascript-object-from-two-arrays) – Heretic Monkey May 13 '21 at 18:21

7 Answers7

6

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);
Alexis
  • 5,681
  • 1
  • 27
  • 44
5

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.

Rohan Orton
  • 1,202
  • 12
  • 14
3

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]]));
Remmar00
  • 196
  • 1
  • 6
1

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);
Manoj Lodhi
  • 978
  • 4
  • 10
1

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.

SimonC
  • 365
  • 3
  • 11
0

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));
mike510a
  • 2,102
  • 1
  • 11
  • 27
0

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