I have a list of objects with two keys, img1
and img2
. I need to identify unique objects based on the values of img1
, while also retaining the corresponding value of img2
.
This is the current code I am using:
const imgs_arr = [
...new Set(
input_arr.map(item => {img_1: item.img1[0]})
)
];
return imgs_arr;
Input Array:
[{img1: ['/path/to/img1'], img2: ['/path/to/img2']},
{img1: ['/path/to/img1'], img2: ['/path/to/img3']},
{img1: ['/path/to/img1'], img2: ['/path/to/img4']},
{img1: ['/path/to/img12'], img2: ['/path/to/img5']},
{img1: ['/path/to/img12'], img2: ['/path/to/img46']},
{img1: ['/path/to/img12'], img2: ['/path/to/img45']},
{img1: ['/path/to/img12'], img2: ['/path/to/img478']}]
Expected Output Array:
[{img1: '/path/to/img1', img2: '/path/to/img2'},
{img1: '/path/to/img12', img2: '/path/to/img5'}]
To provide more context to the question, the goal is to find unique values for img1
and then retrieve the corresponding value for img2
from the first instance.
Your assistance is greatly appreciated!