The original string is:
const strConflictedYesReviewers = "88639280*198719943;88642547*198721749;88627345*198721749;88664734*198721749;88686221*198721749;88676217*198721749;88664734*198721749;88686221*198721749;88676217*198721749;";
The codes extracted from there are:
const reviewerCodesList = strConflictedYesReviewers.split(";")
.map(item => item.split("*"))
.filter(item => !!item[0]);
[
['88639280', '198719943'],
['88642547', '198721749'],
['88627345', '198721749'],
['88664734', '198721749'],
['88686221', '198721749'],
['88676217', '198721749'],
['88664734', '198721749'],
['88686221', '198721749'],
['88676217', '198721749']
]
That’s converted to an object with:
const reviewerCodesObj = reviewerCodesList.map(item => ({ownerid: item[0], opportunityid: item[1]}));
[
{ownerid: '88639280', opportunityid: '198719943'},
{ownerid: '88642547', opportunityid: '198721749'},
{ownerid: '88627345', opportunityid: '198721749'},
{ownerid: '88664734', opportunityid: '198721749'},
{ownerid: '88686221', opportunityid: '198721749'},
{ownerid: '88676217', opportunityid: '198721749'},
{ownerid: '88664734', opportunityid: '198721749'},
{ownerid: '88686221', opportunityid: '198721749'},
{ownerid: '88676217', opportunityid: '198721749'}
]
And lastly, those are combined by unique owners, with:
const conflictedYesReviewers = reviewerCodesObj.reduce(function(acc, curr) {
(acc[curr["ownerid"]] = acc[curr["ownerid"]] || []).push(curr["opportunityid"]);
return acc;
}, {});
[
{88627345: ['198721749']},
{88639280: ['198719943']},
{88642547: ['198721749']},
{88664734: ['198721749', '198721749']},
{88676217: ['198721749', '198721749']},
{88686221: ['198721749', '198721749']}
]
You are wanting to add yes and no information to those quoted id numbers, so let’s add an array to each of those id numbers.
const conflictedYesReviewers = reviewerCodesObj.reduce(function(acc, curr) {
acc[curr.ownerid] = acc[curr.ownerid] || [];
const yesNoObj = {};
yesNoObj[curr.opportunityid] = ["yes, no"];
acc[curr.ownerid].push(yesNoObj);
return acc;
}, {});
That now gives the following structure:
[
{88627345: [
{198721749: ["yes, no"]}
]},
{88639280: [
{198719943: ["yes, no"]}
]},
{88642547: [
{198721749: ["yes, no"]}
]},
{88664734: [
{198721749: ["yes, no"]},
{198721749: ["yes, no"]}
]},
{88676217: [
{198721749: ["yes, no"]},
{198721749: ["yes, no"]}
]},
{88686221: [
{198721749: ["yes, no"]},
{198721749: ["yes, no"]}
]}
]
The updated code in total is:
const strConflictedYesReviewers = "88639280*198719943;88642547*198721749;88627345*198721749;88664734*198721749;88686221*198721749;88676217*198721749;88664734*198721749;88686221*198721749;88676217*198721749;";
const reviewerCodesList = strConflictedYesReviewers.split(";")
.map(item => item.split("*"))
.filter(item => !!item[0]);
const reviewerCodesObj = reviewerCodesList.map(item => ({ownerid: item[0], opportunityid: item[1]}));
const conflictedYesReviewers = reviewerCodesObj.reduce(function(acc, curr) {
acc[curr.ownerid] = acc[curr.ownerid] || [];
const yesNoObj = {};
yesNoObj[curr.opportunityid] = ["yes", "no"];
acc[curr.ownerid].push(yesNoObj);
return acc;
}, {});
console.log(conflictedYesReviewers);
The next thing to figure out from here, is:
- is that structure the kind of thing that you are wanting, and
- where is the information about yes and no choices being obtained from?