Angular从响应中查找数组对象中的重复值?

huangapple go评论62阅读模式
英文:

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中使用过filterreduce函数。

需要找到重复的值,第一个重复的值将保持原样,其他重复的值将转换为空字符串。

英文:

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: &#39;ramesh&#39;, age: 12, city: &#39;mumbai&#39;},
{name: &#39;ramesh&#39;, age: 12, city: &#39;mumbai&#39;},
{name: &#39;amit&#39;, age: 13, city: &#39;mumbai&#39;},
{name: &#39;amit&#39;, age: 13, city: &#39;mumbai&#39;},
{name: &#39;ganesh&#39;, age: 14, city: &#39;mumbai&#39;},
{name: &#39;ganesh&#39;, age: 14, city: &#39;mumbai&#39;},
{name: &#39;ganesh&#39;, age: 14, city: &#39;mumbai&#39;},
{name: &#39;ganesh&#39;, age: 14, city: &#39;mumbai&#39;}
];
const groupedArray = arr.reduce((acc, curr) =&gt; {
const existingObj = acc.find(obj =&gt; obj.name === curr.name &amp;&amp; obj.age === curr.age &amp;&amp; obj.city === curr.city);
if (existingObj) {
acc.push({name: &#39;&#39;, age: &#39;&#39;, city: curr.city});
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(groupedArray);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 01:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定