0

I know this question was answered before multiple times. but i didn't find any solution that helped me out.

I got an array of objects with a Name property. I only want to get the objects with the same name.

How my Array looks like:

[
  {
    Name: 'test',
    coolProperty: 'yeahCool1'
  },
  {
    Name: 'test1',
    coolProperty: 'yeahCool2'
  },
  {
    Name: 'test2',
    coolProperty: 'yeahCool3'
  },
  {
    Name: 'test3',
    coolProperty: 'yeahCool4'
  },
  {
    Name: 'test',
    coolProperty: 'yeahCool5'
  }
]

so I only want to get:

[
  {
    Name: 'test',
    coolProperty: 'yeahCool1'
  },
  {
    Name: 'test',
    coolProperty: 'yeahCool5'
  }
]

I hope someone can help me out :)

3 Answers 3

1

For an O(N) solution, first reduce the array into an object that counts the number of occurrences of each name, and then filter the input by the occurrence count being 2:

const arr = [
  {
    Name: 'test',
    coolProperty: 'yeahCool1'
  },
  {
    Name: 'test1',
    coolProperty: 'yeahCool2'
  },
  {
    Name: 'test2',
    coolProperty: 'yeahCool3'
  },
  {
    Name: 'test3',
    coolProperty: 'yeahCool4'
  },
  {
    Name: 'test',
    coolProperty: 'yeahCool5'
  }
];
const counts = arr.reduce((a, { Name }) => {
  a[Name] = (a[Name] || 0) + 1;
  return a;
}, {});
console.log(arr.filter(({ Name }) => counts[Name] === 2));

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

2 Comments

it works with my fake data sample but doesnt with my real data: jsfiddle.net/hrpg2qy3 why is that?
@newbieTwoPointOH If you wanted the three Marktanalyses, then filter by the count being 3 instead of 2 (or >= 2 instead of === 2)
0

You could use reduce() and filter() method to get the required result.

Using filter() method you need to check if length is greater than 2 then need it will be push in new array inside of reduce() method

DEMO

const arr =[{"Name":"test","coolProperty":"yeahCool1"},{"Name":"test1","coolProperty":"yeahCool2"},{"Name":"test2","coolProperty":"yeahCool3"},{"Name":"test3","coolProperty":"yeahCool4"},{"Name":"test","coolProperty":"yeahCool5"}];

let getCount = (name)=>{
  return arr.filter(o => o.Name == name).length;
}

console.log(arr.reduce((r,item) => {
  let len = getCount(item.Name);
  return r.concat(len>1?item:[]);
}, []));

Comments

0

I see that you have already got an answer. So I thought of adding another way using map.

var counts = {};

var repeats = {};
arr.map(i => {
    counts[i['Name']] = (counts[i['Name']] || []);
    counts[i['Name']].push(i);

    if (counts[i['Name']].length > 1) {
        repeats[i['Name']] = counts[i['Name']];
    }
});

console.log(repeats);

Not the best solution considering the performance. Just wanted to add an alternative method.

Hope it helps!!

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.