如何从reactjs中的数组中删除重复的对象

时间:2019-03-30 07:03:00

标签: javascript arrays reactjs

我想删除状态更改时呈现的数组中的重复对象。

下面,我给出了控制台输出,该输出有14个对象,其中7个是重复的。

我使用过reduce,但是删除了id的{​​{1}}也被删除了...我必须根据jobid进行过滤并删除重复的对象具有相同的jobid

id

4 个答案:

答案 0 :(得分:0)

通过map过滤后,您可以简单地从数组中创建一个jobid,其中将包含唯一值。

let map = {};
array.map(element => {
 if(!map(element.id)){
  map[element.id] = element;
}
})

此后,您可以将值设为Object.values(map)。这将包含唯一元素。

答案 1 :(得分:0)

您只需要基于id和jobid组合进行过滤。为此,您可以按Jobid分组数据并在该Jobid中搜索

const arr = [{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined}]

const temp = arr.reduce((acc, item) => {
   //console.log(item.id, item.jobid);
   if(acc[item.jobid]) {
      //console.log(acc[item.jobid]);
      const idx = acc[item.jobid].findIndex(data => data.id === item.id);
      //console.log(item.jobid, idx)
      if(idx === -1) {
        acc[item.jobid].push(item);
      }
   } else {
      acc[item.jobid] = [item];
   }
   //console.log(acc)
   return acc;
}, {});

let res = []; Object.values(temp).forEach(item => res= res.concat(item));
console.log(res)

答案 2 :(得分:0)

希望这个单人帮忙

const arr = [{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "I1bncvyBsjbu7ePdPvYt", firstName: "Krishnakumar K R", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "design job", empname: "Rakesh"},
{jobid: "atA3Qi4BJu01VrUasiSX", id: "m9SC2DbWxTrlTxJr12p0", firstName: "test user", Title: "design job", empname: "Rakesh"},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "1GMZpFhcOyDSt1KG4abK", firstName: "mohammed basheer", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "MDxEVS2hKftDrrPXsSWL", firstName: "test Candidate", Title: "test job emp", empname: undefined},
{jobid: "nCBNJ1yEn4EaGLR4BHDc", id: "Mmu7oiAoCNcaU2ZWlLeS", firstName: "Rakesh", Title: "test job emp", empname: undefined}]

let pp = arr.filter( (ele, ind) => ind === arr.findIndex( elem => elem.jobid === ele.jobid && elem.id === ele.id))

console.log(pp)

答案 3 :(得分:0)

创建一个tmux new -d -s my-session 'sh -c "my command; sh"' 变量来存储已存在的项将减少循环,而不是在整个数组中搜索数组中的每个项。

tmp