I have an array consisting of more than 50 objects. This array is created by concatenating two arrays together. Each object in this array contains a 'date' key with the date string formatted as:
`"2017-03-31T11:30:00.000Z"`
Additionally, there is a 'caption' key with text associated with each object. Here's an example of elements within the array:
[
{date: "2017-03-31T11:30:00.000Z", caption: "text_1"},
{date: "2016-03-31T11:30:00.000Z", caption: "text_2"},
{date: "2016-03-31T11:30:00.000Z", caption: "text_3"},
{date: "2017-03-31T11:30:00.000Z", caption: "text_4"}
]
In Ruby, a language I am more familiar with, you can map elements in an array and return a new one based on conditions from if statements. I'm curious if JavaScript has similar functionality. Currently, I am iterating through the array and comparing each element to others, but I know this may not be the most efficient way. Ideally, I would like to achieve something like:
let newArray = myArray.map((a, b) => { if (a.date === b.date) { return {text1: a.caption, text2: b.caption}}});
The desired result would look like:
[
{text1: "text_1", text2: "text_4"},
{text1: "text_2", text2: "text_3"}
]
Is there a more elegant and efficient way to accomplish this in JavaScript?
Thank you for your help.