1

I have two list of arrays with country code and currencies in order, but I need to know how to combine and form a object in the following format.

var cn =["AL","DZ","AS","AD","AO","AI","AG","AR","AM","AW","AU"];
var ccy = ["ALL","DZD","USD","EUR","AOA","XCD","XCD","ARS","AMD","AWG","AUD"];

Expected Output:

country:[
{
  "code": "AL",
  "currency": ["ALL"]
},
{
  "code": "DZ",
  "currency": ["DZD"]
}...
...
{
  "code": "AU",
  "currency": ["AUD"]
}
]
  • Where is the rule for matching code and currency? Do they just match 1 on 1? – zynkn Aug 22 '19 at 1:27
  • @zynkn thanks for reply, yes they match 1 on 1 – Senthil Aug 22 '19 at 1:28
  • Use Array methods. What have you tried so far? – user4642212 Aug 22 '19 at 1:29
1

You can use map and use index to access corresponding value from second array

var cn =["AL","DZ","AS","AD","AO","AI","AG","AR","AM","AW","AU"];
var ccy = ["ALL","DZD","USD","EUR","AOA","XCD","XCD","ARS","AMD","AWG","AUD"];

let final = cn.map((code,index)=>{
  let currency = ccy[index]
  return {code, currency: [currency]}
})

console.log(final)

  • 1
    @Quirimmo If you don't agree with code in someone's answer, don't edit it to change the code; instead, write your own answer. – Tyler Roper Aug 22 '19 at 1:41
  • @tyler actually I just changed syntactic sugar, and I thought the changes were suggested and he had to approve them. Sorry guys revert t back – quirimmo Aug 22 '19 at 1:43
  • @quirimmo No worries. Generally a better idea is to suggest/explain the edits in a comment instead :) Also, changing let to const is not "syntactic sugar"; it's a completely different statement, despite the implications not being present in this answer. – Tyler Roper Aug 22 '19 at 1:44
1

You just need to map over one of the two arrays, and use the current index to retrieve the corresponding value in the other array. During each iteration, return the object you want to create:

cc.map((code, index) => ({ code: code, currency: ccy[index]}));
0

One approach might be to iterate the code array using a lambda with an index, then push each JSON object in each iteration.

var cn =["AL","DZ","AS","AD","AO","AI","AG","AR","AM","AW","AU"];
var ccy = ["ALL","DZD","USD","EUR","AOA","XCD","XCD","ARS","AMD","AWG","AUD"];
var country = [];

cn.forEach(function (value, i) {
    country.push({code: value, currency: ccy[i]});
});

console.log(country);

0
var cn =["AL","DZ","AS","AD","AO","AI","AG","AR","AM","AW","AU"];
var ccy = ["ALL","DZD","USD","EUR","AOA","XCD","XCD","ARS","AMD","AWG","AUD"];
var country = [];
for (let i = 0; i < cn.length; i++) {
    country.push({ code: cn[i], currency: ccy[i] });    
}

could do the work

0

You could use a simple .map() for this:

var cn =["AL","DZ","AS","AD","AO","AI","AG","AR","AM","AW","AU"];
var ccy = ["ALL","DZD","USD","EUR","AOA","XCD","XCD","ARS","AMD","AWG","AUD"];
var result = cn.map((code, idx) => ({ code, cn: [ccy[idx]]}));

console.log(result);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.