I have an array like
const arr = [
{id: '1', name: 'name 1', age: 20},
{id: '2', name: 'name 2', age: 21},
{id: '2', name: 'name 3', age: 22},
{id: '2', name: 'name 4', age: 23},
{id: '3', name: 'name 5', age: 24},
{id: '3', name: 'name 6', age: 25},
{id: '4', name: 'name 7', age: 26},
{id: '5', name: 'name 8', age: 27},
{id: '5', name: 'name 9', age: 28},
{id: '5', name: 'name 10', age: 29},
];
I know I have duplicates(looking only id property) in the array and I don't want to remove duplicates instead, I need to append something to differentiate like.
arr = [
{id: '1_0', name: 'name 1', age: 20},
{id: '2_0', name: 'name 2', age: 21},
{id: '2_1', name: 'name 3', age: 22},
{id: '2_2', name: 'name 4', age: 23},
{id: '3_0', name: 'name 5', age: 24},
{id: '3_1', name: 'name 6', age: 25},
{id: '4_0', name: 'name 7', age: 26},
{id: '5_0', name: 'name 8', age: 27},
{id: '5_1', name: 'name 9', age: 28},
{id: '5_2', name: 'name 10', age: 29},
];
Thanks in advance.
CodePudding user response:
You could take an object for counting and map new objects with subversions.
This answer features a closure
(ids => o => ({ ...o, id: `${o.id}_${ids[o.id] ??= 0, ids[o.id] }` }))
({})
where the function is called with an object and returns another function
o => ({ ...o, id: `${o.id}_${ids[o.id] ??= 0, ids[o.id] }` })
for mapping.
The mapping function returns a new object with a replacement of id
property with part of the old id
and a new value which starts with zero for any unseen id
.
const
array = [{ id: '1', name: 'name 1', age: 20 }, { id: '2', name: 'name 2', age: 21 }, { id: '2', name: 'name 3', age: 22 }, { id: '2', name: 'name 4', age: 23 }, { id: '3', name: 'name 5', age: 24 }, { id: '3', name: 'name 6', age: 25 }, { id: '4', name: 'name 7', age: 26 }, { id: '5', name: 'name 8', age: 27 }, { id: '5', name: 'name 9', age: 28 }, { id: '5', name: 'name 10', age: 29 }],
result = array.map(
(ids => o => ({ ...o, id: `${o.id}_${ids[o.id] ??= 0, ids[o.id] }` }))
({})
);
console.log(result);
.as-console-wrapper { max-height:100% !important; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
const arr = [
{ id: "1", name: "name 1", age: 20 },
{ id: "2", name: "name 2", age: 21 },
{ id: "2", name: "name 3", age: 22 },
{ id: "2", name: "name 4", age: 23 },
{ id: "3", name: "name 5", age: 24 },
{ id: "3", name: "name 6", age: 25 },
{ id: "4", name: "name 7", age: 26 },
{ id: "5", name: "name 8", age: 27 },
{ id: "5", name: "name 9", age: 28 },
{ id: "5", name: "name 10", age: 29 },
];
let obj = {};
for (let i = 0; i < arr.length; i ) {
if (obj[arr[i].id] == undefined) {
obj[arr[i].id] = 0;
} else {
obj[arr[i].id] = obj[arr[i].id] 1;
}
arr[i].id = arr[i].id "_" obj[arr[i].id];
}
console.log(arr);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
This will do
CodePudding user response:
Single iteration needed:
const arr = [
{id: '1', name: 'name 1', age: 20},
{id: '2', name: 'name 2', age: 21},
{id: '2', name: 'name 3', age: 22},
{id: '2', name: 'name 4', age: 23},
{id: '3', name: 'name 5', age: 24},
{id: '3', name: 'name 6', age: 25},
{id: '4', name: 'name 7', age: 26},
{id: '5', name: 'name 8', age: 27},
{id: '5', name: 'name 9', age: 28},
{id: '5', name: 'name 10', age: 29},
];
//keep track of how many times each id has been encountered
const counterPerRecord = new Map();
for (const record of arr) {
//get count or default to zero
const next = counterPerRecord.get(record.id) ?? 0;
//update the count for next time we encounter the id
counterPerRecord.set(record.id, next 1);
//update the id
record.id = `_${next}`;
}
console.log(arr);
.as-console-wrapper { max-height:100% !important; }
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
You only need a map to keep track of what the next instance for the next number to append to each id
property. The first time it is zero, and it is increased every time you encounter the same id
.
CodePudding user response:
1) You can easily achieve the result using Map
const arr = [
{ id: "1", name: "name 1", age: 20 },
{ id: "2", name: "name 2", age: 21 },
{ id: "2", name: "name 3", age: 22 },
{ id: "2", name: "name 4", age: 23 },
{ id: "3", name: "name 5", age: 24 },
{ id: "3", name: "name 6", age: 25 },
{ id: "4", name: "name 7", age: 26 },
{ id: "5", name: "name 8", age: 27 },
{ id: "5", name: "name 9", age: 28 },
{ id: "5", name: "name 10", age: 29 },
];
const map = new Map();
arr.forEach((o) => map.has(o.id) ? map.get(o.id).push(o) : map.set(o.id, [o]));
const result = [];
for (let [, arr] of map) {
arr.forEach((o, i) => result.push({ ...o, id: `${o.id}_${i}` }));
}
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
2) You can also use here map with Map (Thank to VLAZ
)
const arr = [
{ id: "1", name: "name 1", age: 20 },
{ id: "2", name: "name 2", age: 21 },
{ id: "2", name: "name 3", age: 22 },
{ id: "2", name: "name 4", age: 23 },
{ id: "3", name: "name 5", age: 24 },
{ id: "3", name: "name 6", age: 25 },
{ id: "4", name: "name 7", age: 26 },
{ id: "5", name: "name 8", age: 27 },
{ id: "5", name: "name 9", age: 28 },
{ id: "5", name: "name 10", age: 29 },
];
const map = new Map();
const result = arr.map((curr) => {
map.set(curr.id, (map.get(curr.id) ?? -1) 1);
return { ...curr, id: `${curr.id}_${map.get(curr.id)}` };
});
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can use somthing like this:
const arr = [
{ id: "1", name: "name 1", age: 20 },
{ id: "2", name: "name 2", age: 21 },
{ id: "2", name: "name 3", age: 22 },
{ id: "2", name: "name 4", age: 23 },
{ id: "3", name: "name 5", age: 24 },
{ id: "3", name: "name 6", age: 25 },
{ id: "4", name: "name 7", age: 26 },
{ id: "5", name: "name 8", age: 27 },
{ id: "5", name: "name 9", age: 28 },
{ id: "5", name: "name 10", age: 29 },
];
for (let i = arr.length - 1; i >= 0; i--) {
arr[i].id = `${arr[i].id}_${arr.filter(el => el.id === arr[i].id).length - 1}`;
}
console.log(arr);
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Notice: as @MrMythical mentioned, 1_0
is just 10
, 1_1
is 11
and so on. So I transform ids into strings in example to avoid id duplications.
Warning: as @VLAZ and @Jeremy Thille noticed, this is expensive solution (O(n^2)
) which is not acceptable for large data. For well-optimized solution (O(n)
), use Map.