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

如何根据相似的id从对象数组中提取对象

如何根据相似的id从对象数组中提取对象

慕姐8265434 2022-12-09 19:42:56
我创建了这个示例代码,以解释我要做什么。arr = [  { id:1 , name:'a', title: 'qmummbw' },  { id:2 , name:'b', title: 'sdmus' },  { id:2 , name:'', title: 'dvfv' },  { id:3 , name:'c', title: 'dujuw' },  { id:1 , name:'d', title: 'ccnyu' },  { id:4 , name:'e', title: 'tjtjn' },  { id:4 , name:'f', title: 'tryr' },  { id:1 , name:'g', title: 'gbgfbgf' },  { id:2 , name:'h', title: 'tgrtg' },  { id:3 , name:'i', title: 'fdvd' },  { id:1 , name:'j', title: 'dsnyc' },  { id:1 , name:'k', title: 'nyuny' }];array = [];allArray = [];for (i = 0; i < arr.length; i++) {  for (j = i + 1; j < arr.length; j++) {    if (arr[i].id === arr[j].id) {      if (!array.includes(arr[i])) {        array.push(arr[i], arr[j]);      } else {        array.push(arr[j]);      }      allArray.push(array);      array = []    }  }}console.log(allArray);我得到的输出是:[[{  id: 1,  name: "a",  title: "qmummbw"}, {  id: 1,  name: "d",  title: "ccnyu"}], [[circular object Object], {  id: 1,  name: "g",  title: "gbgfbgf"}], [[circular object Object], {  id: 1,  name: "j",  title: "dsnyc"}], [[circular object Object], {  id: 1,  name: "k",  title: "nyuny"}], [{  id: 2,  name: "b",  title: "sdmus"}, {  id: 2,  name: "",  title: "dvfv"}], [[circular object Object], {  id: 2,  name: "h",  title: "tgrtg"}], [[circular object Object], [circular object Object]], [{  id: 3,  name: "c",  title: "dujuw"}, {  id: 3,  name: "i",  title: "fdvd"}], [[circular object Object], [circular object Object]], [[circular object Object], [circular object Object]], [[circular object Object], [circular object Object]], [{  id: 4,  name: "e",  title: "tjtjn"}, {  id: 4,  name: "f",  title: "tryr"}], [[circular object Object], [circular object Object]], [[circular object Object], [circular object Object]], [[circular object Object], [circular object Object]]]我的主要目标是获取对象数组,每个对象都包含相同的 id 并记住 arr = [] 可以没有数据。
查看完整描述

3 回答

?
Helenr

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

您可以像这样使用reduce方法:


const filteredArray = arr.reduce((acc, el) => {

  if (acc[el.id] || (acc[el.id] = [])) {

    acc[el.id].push(el);

  }

  return acc;

}, {});


查看完整回答
反对 回复 2022-12-09
?
月关宝盒

TA贡献1772条经验 获得超5个赞

var arr = [

    { id:1 , name:'a', title: 'qmummbw' },

    { id:2 , name:'b', title: 'sdmus' },

    { id:2 , name:'', title: 'dvfv' },

    { id:3 , name:'c', title: 'dujuw' },

    { id:1 , name:'d', title: 'ccnyu' },

    { id:4 , name:'e', title: 'tjtjn' },

    { id:4 , name:'f', title: 'tryr' },

    { id:1 , name:'g', title: 'gbgfbgf' },

    { id:2 , name:'h', title: 'tgrtg' },

    { id:3 , name:'i', title: 'fdvd' },

    { id:1 , name:'j', title: 'dsnyc' },

    { id:1 , name:'k', title: 'nyuny' }

  ],

    result = Object.values(arr.reduce(function (r, a) {

        r[a.id] = r[a.id] || [];

        r[a.id].push(a);

        return r;

    }, Object.create(null))).map(e => e);


console.log([result]);


查看完整回答
反对 回复 2022-12-09
?
RISEBY

TA贡献1856条经验 获得超5个赞

id如有必要,您可以分组并从对象中获取值。


const

    array = [{ id: 1 , name: 'a', title: 'qmummbw' }, { id: 2 , name: 'b', title: 'sdmus' }, { id: 2 , name: '', title: 'dvfv' }, { id: 3 , name: 'c', title: 'dujuw' }, { id: 1 , name: 'd', title: 'ccnyu' }, { id: 4 , name: 'e', title: 'tjtjn' }, { id: 4 , name: 'f', title: 'tryr' }, { id: 1 , name: 'g', title: 'gbgfbgf' }, { id: 2 , name: 'h', title: 'tgrtg' }, { id: 3 , name: 'i', title: 'fdvd' }, { id: 1 , name: 'j', title: 'dsnyc' }, { id: 1 , name: 'k', title: 'nyuny' }],

    grouped = array.reduce((r, o) => ((r[o.id] = r[o.id] || []).push(o), r), {});


console.log(grouped);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2022-12-09
  • 3 回答
  • 0 关注
  • 120 浏览
慕课专栏
更多

添加回答

举报

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