jawahar j
jawahar j

Reputation: 303

Compare and map the object using angular 4

I have 2 objects, in this mydata object i have to countrycode to country name. country name i have to pick it from countries object.

countries = [
  {
    "name": "Afghanistan",
    "code": "AF",
    "localizationLang": "en"
  },
  {
    "name": "Albania",
    "code": "AL",
    "localizationLang": "en"
  }
];

mydata = [{
    "orgCode": "ZC27693542",
    "orgName": "B - Sedex Conference Demo Purpose Only",
    "siteCode": null,
    "siteName": null,
    "countryCode": "AF",
    "state": null,
    "product": [],
    "productArea": [],
    "lastDateAudit": null,
    "saqPercentage": 0.0,
    "auditCode": null
},...
];

And i want the output object should be

[{
    "orgCode": "ZC27693542",
    "orgName": "B - Sedex Conference Demo Purpose Only",
    "siteCode": null,
    "siteName": null,
    "countryCode": "Afghanistan",
    "state": null,
    "product": [],
    "productArea": [],
    "lastDateAudit": null,
    "saqPercentage": 0.0,
    "auditCode": null
},...
]

I need to change the country code to country name.

Upvotes: 0

Views: 222

Answers (2)

Mars.Tsai
Mars.Tsai

Reputation: 184

try it

var countries = [{
  "name": "Afghanistan",
  "code": "AF",
  "localizationLang": "en"
}, {
  "name": "Albania",
  "code": "AL",
  "localizationLang": "en"
}];

var mydata = [{
  "orgCode": "ZC27693542",
  "orgName": "B - Sedex Conference Demo Purpose Only",
  "siteCode": null,
  "siteName": null,
  "countryCode": "AF",
  "state": null,
  "product": [],
  "productArea": [],
  "lastDateAudit": null,
  "saqPercentage": 0.0,
  "auditCode": null
}, {
  "orgCode": "ZC27695039",
  "orgName": "Sonata-AB-Member",
  "siteCode": "ZS27695161",
  "siteName": "UEM group",
  "countryCode": "AL",
  "state": null,
  "product": [],
  "productArea": ["50203200", "50201700", "50202300"],
  "lastDateAudit": null,
  "saqPercentage": 13.0,
  "auditCode": null
}];

var newArr = mydata.map(function(value) {
  var country = countries.find(function(countrie) {
    return countrie.code == value.countryCode;
  });

  value.countryCode = country.name;
  return value;
});

console.log(newArr)

demo:https://codepen.io/anon/pen/QmeEXe

Upvotes: 0

Saso
Saso

Reputation: 148

Easy :)

mydata.map(x=>{
x.countryCode = countries.find(c=>c.code === x.countryCode).name; 
return x;
})

Upvotes: 2

Related Questions