I need to combine the LAST fromAddress
and toAddresses
values into just one set of array without duplication and without the loginUser
.
Expected Output
{ "newResponse": ["[email protected]", "[email protected]"] }
const loginUser = "[email protected]"
const old = [
{
"toAddresses": ["[email protected]", "[email protected]"],
},
{
"fromAddress": "[email protected]",
"toAddresses": ["[email protected]", "[email protected]"],
}
];
let emailLength = old?.length - 1
let email = old[emailLength]
const newResponse = Array.from(new Set([...email.map(x => [...x.toAddresses].concat([...x.fromAddress || []]))]))
console.log(newResponse)
CodePudding user response:
I'm trying to write an answer, but you constantly re-edit your post and reset the question. Also, it's poorly constructed. What does this even mean?
LAST fromAddress and toAddresses values into just one set of array without duplication
The last values of two separate arrays implies that there are exactly two values, so, how you could possible face a duplication here?
Downvoted.
CodePudding user response:
You're trying to call map
on an object, but is only an array method.
You can just access the properties directly on the email
object, since you know what you're looking for you don't need to iterate on it.
Then filter the array for the logged in user and construct a new object for the response.
const loginUser = "[email protected]"
const old = [
{
"toAddresses": ["[email protected]", "[email protected]"],
},
{
"fromAddress": "[email protected]",
"toAddresses": ["[email protected]", "[email protected]"],
}
];
let emailLength = old?.length - 1
let email = old[emailLength]
let addresses = Array.from(new Set([...email.toAddresses, email.fromAddress]))
addresses = addresses.filter(addy => addy !== loginUser)
const newResponse = { "newResponse": addresses }
console.log(newResponse)
CodePudding user response:
const loginUser = "[email protected]"
const old = [
{
"toAddresses": ["[email protected]", "[email protected]"],
},
{
"fromAddress": "[email protected]",
"toAddresses": ["[email protected]", "[email protected]"],
}
];
let last = old[old.length - 1];
let combined = new Set([last.fromAddress, ...last.toAddresses]);
combined.delete(loginUser);
let newResponse = Array.from(combined);
console.log(newResponse);