I have an array of insurance policy objects. The policy object has several fields, one of which is policyNumber. I am trying to create a string of the policy numbers using JavaScript.
My code thus far is as follows:
const policies = [
{
name: "betty",
policyNumber: "12345",
policyKindCode: "ABC"
},
{
name: "debbie",
policyNumber: "67890",
policyKindCode: "DEF"
},
{
name: "judy",
policyNumber: "13579",
policyKindCode: "GHI"
},
{
name: "rudy",
policyNumber: "24680",
policyKindCode: "JKL"
}
]
const policiesString = policies.reduce((str, policy) => {
str += (policy.policyNumber + ", ");
return str;
});
console.log('policiesString = ' + policiesString);
The console.log is returning the following:
"policiesString = [object Object]67890, 13579, 24680, "
If I comment out the first object it just puts the [object Object] where 67890 would go and then has the last two policy numbers, and so on.
What am I doing wrong?