从 Javascript 对象数组中的重复项创建唯一值
Create unique values from duplicates in Javascript array of objects
我在 Javascript 中有一组重复的对象。我想通过添加单个值的出现索引来创建一个唯一对象数组。
这是我的初始数据:
const array= [
{name:"A"},
{name:"A"},
{name:"A"},
{name:"B"},
{name:"B"},
{name:"C"},
{name:"C"},
];
这是预期的最终结果:
const array= [
{name:"A-0"},
{name:"A-1"},
{name:"A-2"},
{name:"B-0"},
{name:"B-1"},
{name:"C-0"},
{name:"C-1"},
];
我觉得这应该很简单,但卡了一会儿。你能告诉我如何去做吗?另外,如果可能的话,我需要它高效,因为数组最多可以容纳 1000 个项目。
编辑:这是我的解决方案,但我觉得效率不高。
const array = [
{ name: "A" },
{ name: "A" },
{ name: "C" },
{ name: "B" },
{ name: "A" },
{ name: "C" },
{ name: "B" },
];
const sortedArray = _.sortBy(array, 'name');
let previousItem = {
name: '',
counter: 0
};
const indexedArray = sortedArray.map((item) => {
if (item.name === previousItem.name) {
previousItem.counter += 1;
const name = `${item.name}-${previousItem.counter}`;
return { name };
} else {
previousItem = { name: item.name, counter: 0};
return item;
}
});
保留一个计数器,如果当前名称改变,重置计数器。
这个版本改变了对象。不确定你是否想要一份副本。您可以先按对象名称对数组进行排序,以确保它们是有序的(如果这还不是现有的先决条件。)
const array = [
{ name: "A" },
{ name: "A" },
{ name: "A" },
{ name: "B" },
{ name: "B" },
{ name: "C" },
{ name: "C" },
];
let name, index;
for (let i in array) {
index = array[i].name == name ? index + 1 : 0;
name = array[i].name;
array[i].name += `-${index}`;
}
console.log(array);
如果您不想排序,也不想改变任何对象,另一种方法是使用映射并跟踪每个对象的当前索引。
const array = [
// NOTE: I put the items in mixed up order.
{ name: "A" },
{ name: "C" },
{ name: "A" },
{ name: "B" },
{ name: "A" },
{ name: "C" },
{ name: "B" },
];
let index = {};
let next = name => index[name] = index[name] + 1 || 0;
let result = array.map(obj => ({ ...obj, name: obj.name + '-' + next(obj.name) }));
console.log(result);
目前您是先排序再循环,这可能不是最有效的解决方案。
我建议你用一个帮助对象映射它。
const a = [{name:"A"},{name:"A"},{name:"A"},{name:"B"},{name:"B"},{name:"C"},{name:"C"},], o = {};
const r = a.map(({ name }) => {
typeof o[name] === 'number' ? o[name]++ : o[name] = 0;
return { name: `${name}-${o[name]}` };
});
console.log(r);
我在 Javascript 中有一组重复的对象。我想通过添加单个值的出现索引来创建一个唯一对象数组。
这是我的初始数据:
const array= [
{name:"A"},
{name:"A"},
{name:"A"},
{name:"B"},
{name:"B"},
{name:"C"},
{name:"C"},
];
这是预期的最终结果:
const array= [
{name:"A-0"},
{name:"A-1"},
{name:"A-2"},
{name:"B-0"},
{name:"B-1"},
{name:"C-0"},
{name:"C-1"},
];
我觉得这应该很简单,但卡了一会儿。你能告诉我如何去做吗?另外,如果可能的话,我需要它高效,因为数组最多可以容纳 1000 个项目。
编辑:这是我的解决方案,但我觉得效率不高。
const array = [
{ name: "A" },
{ name: "A" },
{ name: "C" },
{ name: "B" },
{ name: "A" },
{ name: "C" },
{ name: "B" },
];
const sortedArray = _.sortBy(array, 'name');
let previousItem = {
name: '',
counter: 0
};
const indexedArray = sortedArray.map((item) => {
if (item.name === previousItem.name) {
previousItem.counter += 1;
const name = `${item.name}-${previousItem.counter}`;
return { name };
} else {
previousItem = { name: item.name, counter: 0};
return item;
}
});
保留一个计数器,如果当前名称改变,重置计数器。
这个版本改变了对象。不确定你是否想要一份副本。您可以先按对象名称对数组进行排序,以确保它们是有序的(如果这还不是现有的先决条件。)
const array = [
{ name: "A" },
{ name: "A" },
{ name: "A" },
{ name: "B" },
{ name: "B" },
{ name: "C" },
{ name: "C" },
];
let name, index;
for (let i in array) {
index = array[i].name == name ? index + 1 : 0;
name = array[i].name;
array[i].name += `-${index}`;
}
console.log(array);
如果您不想排序,也不想改变任何对象,另一种方法是使用映射并跟踪每个对象的当前索引。
const array = [
// NOTE: I put the items in mixed up order.
{ name: "A" },
{ name: "C" },
{ name: "A" },
{ name: "B" },
{ name: "A" },
{ name: "C" },
{ name: "B" },
];
let index = {};
let next = name => index[name] = index[name] + 1 || 0;
let result = array.map(obj => ({ ...obj, name: obj.name + '-' + next(obj.name) }));
console.log(result);
目前您是先排序再循环,这可能不是最有效的解决方案。
我建议你用一个帮助对象映射它。
const a = [{name:"A"},{name:"A"},{name:"A"},{name:"B"},{name:"B"},{name:"C"},{name:"C"},], o = {};
const r = a.map(({ name }) => {
typeof o[name] === 'number' ? o[name]++ : o[name] = 0;
return { name: `${name}-${o[name]}` };
});
console.log(r);