I have the following array of objects and I need to identify unique objects from this array based on the key img1. I was able to identify unique values associated to the key img1 but not the associated value of the key img2.
Code I have currently,
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'}]
Adding some more color to the question based on the questions am getting in the comments.
img1 key has values from which I need to find unique values and then find the corresponding value of key img2 from the first match.
Your help is greatly appreciated!
img2matching the first instance of keyimg1