admin管理员组

文章数量:1336117

I have object array like this.

const array = [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 12 } ]

I want to count duplicates objects and store the count as new object field.

I found this snippet and it work great but it not exactly what i need.

const names = [{  _id: 1 }, { _id: 1}, { _id: 2}, { _id: 1}]

    const result = [...names.reduce( (mp, o) => {
    if (!mp.has(o._id)) mp.set(o._id, Object.assign({ count: 0 }, o));
    mp.get(o._id).count++;
    return mp;
    }, new Map).values()];

    console.log(result);

It works with object with one field _id. In my case there are two, x and y

How should I modify that code?

In brief...I would like to receive the result:

result = [ { x: 1, y: 2, count:3 }, { x: 3, y: 4, count:2 }, { x: 3, y: 12, count:1 } ]

I have object array like this.

const array = [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 12 } ]

I want to count duplicates objects and store the count as new object field.

I found this snippet and it work great but it not exactly what i need.

const names = [{  _id: 1 }, { _id: 1}, { _id: 2}, { _id: 1}]

    const result = [...names.reduce( (mp, o) => {
    if (!mp.has(o._id)) mp.set(o._id, Object.assign({ count: 0 }, o));
    mp.get(o._id).count++;
    return mp;
    }, new Map).values()];

    console.log(result);

It works with object with one field _id. In my case there are two, x and y

How should I modify that code?

In brief...I would like to receive the result:

result = [ { x: 1, y: 2, count:3 }, { x: 3, y: 4, count:2 }, { x: 3, y: 12, count:1 } ]
Share Improve this question edited Dec 13, 2017 at 11:00 bastej asked Dec 12, 2017 at 14:11 bastejbastej 831 silver badge5 bronze badges 2
  • Possible duplicate of How to count duplicate value in an array in javascript – Jared Smith Commented Dec 12, 2017 at 14:13
  • 1 You can use JSON.stringify([o.x, o.y]) or o.x+'|'+o.y instead of o._id - anything that uniquely identifies your objects and fits your data types – Bergi Commented Dec 12, 2017 at 14:18
Add a ment  | 

2 Answers 2

Reset to default 6

You can use Object.values() and reduce() methods to return new array of objects.

const array = [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 12 } ]

const result = Object.values(array.reduce((r, e) => {
  let k = `${e.x}|${e.y}`;
  if(!r[k]) r[k] = {...e, count: 1}
  else r[k].count += 1;
  return r;
}, {}))

console.log(result)

Here is the solution with Map and spread syntax ...

const array = [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 12 } ]

const result = [...array.reduce((r, e) => {
  let k = `${e.x}|${e.y}`;
  if(!r.has(k)) r.set(k, {...e, count: 1})
  else r.get(k).count++
  return r;
}, new Map).values()]

console.log(result)

One way to do it would be to create an index mapping both x and y to the result entry:

let index = { };
let result = [ ];
const array = [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 12 } ];
array.forEach(point => {
    let key = '' + point.x + '||' + point.y;
    if (key in index) {
        index[key].count++;
    } else {
        let newEntry = { x: point.x, y: point.y, count: 1 };
        index[key] = newEntry;
        result.push(newEntry);
    }
});
console.log(result);

本文标签: ecmascript 6JavascriptCounting duplicates in array of object with many fields in es6Stack Overflow