英文:
angular find duplicate values of array of objects from response?
问题
以下是要翻译的内容:
// 从响应中对对象数组进行分组。
[
{ name: ramesh, age: 12, city: mumbai },
{ name: ramesh, age: 12, city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
];
我想要按如下方式分组。
[
{ name: ramesh, age: 12, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: "", age: "", city: mumbai },
];
我尝试在Angular中使用过filter
和reduce
函数。
需要找到重复的值,第一个重复的值将保持原样,其他重复的值将转换为空字符串。
英文:
Group array of objects from response.
[
{ name: ramesh, age: 12, city: mumbai },
{ name: ramesh, age: 12, city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
];
I want to group like this.
[
{ name: ramesh, age: 12, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: "", age: "", city: mumbai },
];
I tried with filter, reduce functions in Angular
need to find duplicate value and first value of duplicate will show exact and other duplicates value will convert in empty string
答案1
得分: 0
使用数组的`reduce`方法创建一个包含分组数据的新对象数组
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [
{name: 'ramesh', age: 12, city: 'mumbai'},
{name: 'ramesh', age: 12, city: 'mumbai'},
{name: 'amit', age: 13, city: 'mumbai'},
{name: 'amit', age: 13, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'}
];
const groupedArray = arr.reduce((acc, curr) => {
const existingObj = acc.find(obj => obj.name === curr.name && obj.age === curr.age && obj.city === curr.city);
if (existingObj) {
acc.push({name: '', age: '', city: curr.city});
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(groupedArray);
<!-- end snippet -->
英文:
use the reduce
method of arrays to create a new array of objects with the grouped data
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [
{name: 'ramesh', age: 12, city: 'mumbai'},
{name: 'ramesh', age: 12, city: 'mumbai'},
{name: 'amit', age: 13, city: 'mumbai'},
{name: 'amit', age: 13, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'}
];
const groupedArray = arr.reduce((acc, curr) => {
const existingObj = acc.find(obj => obj.name === curr.name && obj.age === curr.age && obj.city === curr.city);
if (existingObj) {
acc.push({name: '', age: '', city: curr.city});
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(groupedArray);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论