猿问

如何在reactjs中显示地图内对象数组中的数据而不重复

在reactjs中需要一些帮助


我下面有这些数据,我想显示它而不重复,这意味着它应该是“A”、“B”和“D”的单个类别,我想在下面的地图中执行此操作。


 const person = {

   con: [

    { category: "A", name: "john"  },

    { category: "A", name: "john" },

    { category: "B", name: "rahul" },

    { category: "B", name: "jay" },

    { category: "C", name: "dave" },

    { category: "D", name: "alex" },

    { category: "D", name: "alex" },

    { category: "E", name: "sam1" },

    { category: "F", name: "sam2" },

    { category: "G", name: "sam3" },

   ]

 };



person.con && person.con.map((data, index) => ( 

  console.log(data, 'data')   

  // I want to display results here...

 

))

预期输出:


{ category: "A", name: "john"  },

{ category: "B", name: "rahul" },

{ category: "C", name: "dave" },

{ category: "D", name: "alex" },

{ category: "E", name: "sam1" },

{ category: "F", name: "sam2" },

{ category: "G", name: "sam3" },

这可能吗?谢谢。


SMILET
浏览 106回答 2
2回答

慕盖茨4494581

这是示例代码const person = {    con: [     { category: "A", name: "john"  },     { category: "A", name: "john" },     { category: "B", name: "rahul" },     { category: "B", name: "jay" },     { category: "C", name: "dave" },     { category: "D", name: "alex" },     { category: "D", name: "alex" },     { category: "E", name: "sam1" },     { category: "F", name: "sam2" },     { category: "G", name: "sam3" },    ]  };const duplicateCheck = []; person.con && person.con.map((data, index) => {    if (duplicateCheck.includes(data.category))        return null;    duplicateCheck.push(data.category);    return data;}).filter((e)=>(e))// Above code returns filtered out array输出:  [ { category: 'A', name: 'john' },  { category: 'B', name: 'rahul' },  { category: 'C', name: 'dave' },  { category: 'D', name: 'alex' },  { category: 'E', name: 'sam1' },  { category: 'F', name: 'sam2' },  { category: 'G', name: 'sam3' } ]

狐的传说

我认为它应该看起来像这样:稍微重组您的数据以使其更容易渲染然后渲染它。const person = {&nbsp; con: [&nbsp; &nbsp; { category: "A", name: "john" },&nbsp; &nbsp; { category: "A", name: "doe" },&nbsp; &nbsp; { category: "B", name: "rahul" },&nbsp; &nbsp; { category: "B", name: "jay" },&nbsp; &nbsp; { category: "C", name: "dave" },&nbsp; &nbsp; { category: "D", name: "alex" },&nbsp; &nbsp; { category: "D", name: "devid" },&nbsp; &nbsp; { category: "E", name: "sam" },&nbsp; &nbsp; { category: "F", name: "sam" },&nbsp; &nbsp; { category: "G", name: "sam" }&nbsp; ]};const groupedByCategory = person.con.reduce((acc, item) => {&nbsp; if (!Array.isArray(acc[item.category])) {&nbsp; &nbsp; acc[item.category] = [];&nbsp; }&nbsp; acc[item.category].push(item.name);&nbsp; return acc;}, {});const Comp = () => {&nbsp; return Object.entries(groupedByCategory).map(([category, names]) => {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <h3>{category}</h3>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {names.map((name) => (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>{name}</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ))}&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; });};
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答