Shreerang
Shreerang

Reputation: 387

Identify unique objects in array based on a specific value

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!

Upvotes: 1

Views: 112

Answers (2)

Azad
Azad

Reputation: 5272

function filterArrayByImg1(arr) {

  let x = [];

  return arr.filter((e, a) => {

      if (!e.img1 || !e.img1[0] || x.includes(e.img1[0]))
        return false;
      else {
        x.push(e.img1[0]);
        return true;
      }
    })
    .map(e => ({
      img1: e.img1[0],
      img2: e.img2[0]
    }));
}


let inputArray = [{
    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']
  }
];



//filter the array
let filteredArr = filterArrayByImg1(inputArray);

console.log(filteredArr);

Upvotes: 1

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11011

Use forEach loop and build any object with unqiue key. Get Object.values from built object.

const data = [
  { 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"] }
];

const update = data => {
  const res = {};

  data.forEach(item => {
    const u_key = item.img1[0];
    if (!(u_key in res)) {
        res[u_key] = item;
    }
  });
  return Object.values(res);
};

console.log(update(data));

Upvotes: 1

Related Questions