I am looking to transform a list of email addresses into a key-value object within an array. Starting with
"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="40252d21292c002d21292c6e232f2d">[email protected]</a>,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd90949698bd98859c908d9198d39e9290">[email protected]</a>,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e3e6e1e7c9eee4e8e0e5a7eae6e4">[email protected]</a>"
and aiming to achieve
[
{"to": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89ece4e8e0e5c9e4e8e0e5a7eae6e4">[email protected]</a>"},
{"to": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd6d2d0defbdec3dad6cbd7de95d8d4d6">[email protected]</a>"},
{"to": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0dadfd8def0d7ddd1d9dc9ed3dfdd">[email protected]</a>"}
]
This is the approach I took:
var req = {
query: {
personalize:
"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="51343c30383d113c30383d7f323e3c">[email protected]</a>,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d303436381d38253c302d3138733e3230">[email protected]</a>,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c262324220c2b212d2520622f2321">[email protected]</a>"
}
};
var emailList = req.query.personalize;
var emailArr = emailList.split(",");
var emailObj = Object.assign({}, emailArr);
console.log(emailObj);
Here's the outcome of my efforts:
"0": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8bdb5b9b1b498b5b9b1b4f6bbb7b5">[email protected]</a>"
"1": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0703010f2a0f120b071a060f44090507">[email protected]</a>"
"2": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef85808781af88828e8683c18c8082">[email protected]</a>"
Following that, I attempted this variation:
var req = {
query: {
personalize:
"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89d95999194b895999194d69b9795">[email protected]</a>,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cf1f5f7f9dcf9e4fdf1ecf0f9b2fff3f1">[email protected]</a>,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d8dddadcf2d5dfd3dbde9cd1dddf">[email protected]</a>"
}
};
var emailList = req.query.personalize;
var arr = emailList.split(",");
const res = arr.reduce((acc,curr)=> (acc[curr]='to',acc),{});
console.log(res)
Although it was close, the result ended up in reverse of what I expected, displaying like so:
"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e7b737f77725e737f7772307d7173">[email protected]</a>": "to"
...