var arrayData=[
{
amount:10,gameId:7 ,consoleId:3 id: 1
},
{
amount:5, gameId:18 ,consoleId:3 id: 2
},
{
amount:5, gameId:18 ,consoleId:3 id: 3
},
{
amount:10, gameId:7 ,consoleId:3 id: 4
},
{
amount:10, gameId:7 ,consoleId:4 id: 5
},
{
amount:15, gameId:7 ,consoleId:3 id: 6
}
]
matching will be on amount,gameId,consoleId and it return their Id by grouping same records. output like this
[[2,3],[1,4]]
lodash or without lodash
use amount and gameId and consoleId is key, then together group by key.
like:
const key = `${item.amount}-${item.gameId}-${item.consoleId}`;
full answer:
var getIdGroup = (arr) => {
const mp = new Map();
arr.forEach(item => {
const key = `${item.amount}-${item.gameId}-${item.consoleId}`;
const value = mp.get(key);
if (value) {
mp.set(key, value.concat(item.id));
} else {
mp.set(key, [item.id]);
}
});
// only filter length >= 2
return [...mp.values()].filter(item => item.length>=2);
}
var arrayData = [{
amount: 10,
gameId: 7,
consoleId: 3,
id: 1,
},
{
amount: 5,
gameId: 18,
consoleId: 3,
id: 2,
},
{
amount: 5,
gameId: 18,
consoleId: 3,
id: 3,
},
{
amount: 10,
gameId: 7,
consoleId: 3,
id: 4,
},
{
amount: 10,
gameId: 7,
consoleId: 4,
id: 5,
},
{
amount: 15,
gameId: 7,
consoleId: 3,
id: 6,
},
];
var getIdGroup = (arr) => {
const mp = new Map();
arr.forEach((item) => {
const key = `${item.amount}-${item.gameId}-${item.consoleId}`;
const value = mp.get(key);
if (value) {
mp.set(key, value.concat(item.id));
} else {
mp.set(key, [item.id]);
}
});
// only filter length >= 2
return [...mp.values()].filter((item) => item.length >= 2);
};
console.log(getIdGroup(arrayData));