My inventory of wholesalers looks like this:
const wholesalers = [
"B536 - T QUALITY LTD",
];
I also have a collection of JSON objects as shown below:
{
'Preferred Wholesaler': 'T-Quality'
},
{
'Preferred Wholesaler': 'T Quality'
},
I need help figuring out how to match the value of 'Preferred Wholesaler' in the JSON object with one of the values in the array. Once matched, I want to update the JSON object with the corresponding value from the array, for example: 'Preferred Wholesaler': 'B536 - T QUALITY LTD'.
This is what I have tried so far:
// Ignoring the first 7 characters of the wholesaler and removing " LTD" from the end:
var newWholesalers = wholesalers.map(wholesaler => {
return wholesaler.substring(7).replace(" LTD", "")
});
// Processing JSON Data
var newRecords = data.map(record => {
record["Preferred Wholesaler"] = record["Preferred Wholesaler"].toUpperCase()
// Checking if preferred wholesaler exists in the array
// If yes, updating with 'True', else setting it as 'False'
if (newWholesalers.includes(record["Preferred Wholesaler"])) {
return {
...record,
["Preferred Wholesaler"]: 'True'
}
} else {
return {
...record,
["Preferred Wholesaler"]: 'False'
}
}
});
I am looking for a solution to make the includes() function return the matched value instead of true/false. Any suggestions?