-1

I did search on Stack Overflow, but I am still confused.

There are two JavaScript arrays: a and b

var a = ['US','UK'];
var b = [
    US: 'United States',
    UK: 'United Kingdom',
    CN: 'China',
    JP: 'Japan'
];

How to get 'United states' and 'United Kingdom' by 'US' and 'UK', then create a new array c like below?

var c = [
    US: 'United States',
    UK: 'United Kingdom'
]
BadJohnny
  • 331
  • 6
  • 19

3 Answers3

2

var a = ['US','UK'];
var b = {
         'US': 'United States',
         'UK': 'United Kingdom',
         'CN': 'China',
         'JP': 'Japan'
         };

one = {};
for (var key of a){
   one[key] = b[key];
}
console.log(one);


two = {};
for (let i = 0; i < a.length; i++){
    two[a[i]] = b[a[i]];
}
console.log(two);

three = {};
a.forEach(function(el){
    three[el] = b[el];
});
console.log(three);

As pointed out in the comments, your b & c are not valid JavaScript arrays. If you need key value pairs, you need to use an Object - which uses curly braces {} to enclose the key:value pairs.

Assuming

// b holds the master list of key-value pairs 
// from b, you will fetch the pairs with keys present in a`

Input:-

var a = ['US','UK'];
var b = {
         'US': 'United States',
         'UK': 'United Kingdom',
         'CN': 'China',
         'JP': 'Japan'};

and required:-
c = {    'US': 'United States',
         'UK': 'United Kingdom',
}

You can try

// Traditional approach using a normal for loop
c = {};
for (let i = 0; i < a.length; i++){
    c[a[i]] = b[a[i]];
}

// Slightly modern forEach approach
c = {};
a.forEach(function(el){
    c[el] = b[el];
});

// Modern approach using for...of loop
c = {};
for (let key of a){  
   c[key] = b[key];
}
Sarath Chandra
  • 1,850
  • 19
  • 40
  • 1
    I definitely suggest reading up on `for...of` loops as they are relatively new, very useful, but potentially not all that straight forward for someone new to javascript. – TheCrzyMan Feb 01 '18 at 13:35
  • As @TheCrzyMan pointed out, read this top answer on more about iterating on array like objects: https://stackoverflow.com/a/9329476/2869791 – Sarath Chandra Feb 01 '18 at 13:57
  • Thanks, I'm new to javascript, thanks for your suggestion!! I will read the basic syntax usage. – BadJohnny Feb 01 '18 at 15:35
1

Input

var a = ['US', 'UK'];

//b must be object
var b = {
     'US': 'United States',
     'UK': 'United Kingdom',
     'CN': 'China',
     'JP': 'Japan'
};

Result

var c = {};

for (var i=0; i < a.length; i++)
{
    c[ a[i] ] = b[ a[i] ];
}

or even simplier

var c = {};

for (var i of a)
{
    c[ i ] = b[ i ];
}
Sinisa Bobic
  • 1,311
  • 10
  • 15
-1
Another way using underscoreJS(-.each) //\\ http://underscorejs.org/
====================================================================
var a = ['US','UK'];
var b = [{
         'US': 'United States',
         'UK': 'United Kingdom',
         'CN': 'China',
         'JP': 'Japan'
}];
var count = 0;
var c = {};
_.each(b[0], function(v,k){
    if(a[count] == k){
        c[a[count]] = v;
    }
    count++;
});
console.log(c);