为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

SMILET 2023-07-14 09:39:18
在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" },这可能吗?谢谢。
查看完整描述

2 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

这是示例代码


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' } ]


查看完整回答
反对 回复 2023-07-14
?
狐的传说

TA贡献1804条经验 获得超3个赞

我认为它应该看起来像这样:稍微重组您的数据以使其更容易渲染然后渲染它。


const person = {

  con: [

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

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

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

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

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

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

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

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

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

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

  ]

};


const groupedByCategory = person.con.reduce((acc, item) => {

  if (!Array.isArray(acc[item.category])) {

    acc[item.category] = [];

  }

  acc[item.category].push(item.name);

  return acc;

}, {});


const Comp = () => {

  return Object.entries(groupedByCategory).map(([category, names]) => {

    return (

      <div>

        <h3>{category}</h3>

        <ul>

          {names.map((name) => (

            <li>{name}</li>

          ))}

        </ul>

      </div>

    );

  });

};


查看完整回答
反对 回复 2023-07-14
  • 2 回答
  • 0 关注
  • 96 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信