1

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!

2
  • What is the pattern resulting path/to/img2 and path/to/img5 as a value to img2 properties? Commented Mar 11, 2020 at 5:23
  • I need to extract the first value of key img2 matching the first instance of key img1 Commented Mar 11, 2020 at 5:30

2 Answers 2

1

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));

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.