const arr= [
["google.com|toster.ru|RU|120|222"],
["google.com|toster.ru|RU|120|222"],
["google.com|toster.ru|UA|120|222"],
["google.com|toster.ru|BG|120|222"],
["google.com|yandex.ru|IT|777|843"]
];
const groupBy = 2;
const filtered = Array.from(new Set(arr.map(arr => arr[0])))
.map(str => str.split('|'))
.reduce((acc, curArr) => {
const index = acc.findIndex(curAcc => {
return !curAcc.some((curAccEl, i) => curAccEl !== curArr[i] && i !== groupBy);
});
if (index === -1) {
acc.push(curArr);
}
else {
acc[index][groupBy] += '+' + curArr[groupBy];
}
return acc;
}, [])
.map(arr => [arr.join('|')]);
const arr = [
"google.com|toster.ru|RU|120|222",
"google.com|toster.ru|RU|120|222",
"google.com|toster.ru|UA|120|222",
"google.com|toster.ru|BG|120|222",
"google.com|yandex.ru|IT|777|843"
];
const result = arr.reduce((acc, curr) => {
const [a, b, geo, c, id] = curr.split('|');
const elem = acc.find(({value}) => value === id);
if (elem) {
if (!elem.items.includes(geo)) {
elem.items.push(geo);
}
} else {
acc.push({
items: [geo],
elems: [a, b, c],
value: id
});
}
return acc;
}, []).map(({items, elems: [a, b, c], value}) => [a, b, items.join('+'), c, value].join('|'));