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

我创建了这个示例代码,以解释我要做什么。


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 = [] 可以没有数据。


慕姐8265434
浏览 148回答 3
3回答

Helenr

您可以像这样使用reduce方法:const filteredArray = arr.reduce((acc, el) => {&nbsp; if (acc[el.id] || (acc[el.id] = [])) {&nbsp; &nbsp; acc[el.id].push(el);&nbsp; }&nbsp; return acc;}, {});

月关宝盒

var arr = [&nbsp; &nbsp; { id:1 , name:'a', title: 'qmummbw' },&nbsp; &nbsp; { id:2 , name:'b', title: 'sdmus' },&nbsp; &nbsp; { id:2 , name:'', title: 'dvfv' },&nbsp; &nbsp; { id:3 , name:'c', title: 'dujuw' },&nbsp; &nbsp; { id:1 , name:'d', title: 'ccnyu' },&nbsp; &nbsp; { id:4 , name:'e', title: 'tjtjn' },&nbsp; &nbsp; { id:4 , name:'f', title: 'tryr' },&nbsp; &nbsp; { id:1 , name:'g', title: 'gbgfbgf' },&nbsp; &nbsp; { id:2 , name:'h', title: 'tgrtg' },&nbsp; &nbsp; { id:3 , name:'i', title: 'fdvd' },&nbsp; &nbsp; { id:1 , name:'j', title: 'dsnyc' },&nbsp; &nbsp; { id:1 , name:'k', title: 'nyuny' }&nbsp; ],&nbsp; &nbsp; result = Object.values(arr.reduce(function (r, a) {&nbsp; &nbsp; &nbsp; &nbsp; r[a.id] = r[a.id] || [];&nbsp; &nbsp; &nbsp; &nbsp; r[a.id].push(a);&nbsp; &nbsp; &nbsp; &nbsp; return r;&nbsp; &nbsp; }, Object.create(null))).map(e => e);console.log([result]);

RISEBY

id如有必要,您可以分组并从对象中获取值。const&nbsp; &nbsp; 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' }],&nbsp; &nbsp; 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; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript