简体   繁体   English

如何在 javascript 中检索具有相同属性值的对象数组

[英]How to retrieve the array of objects having same property value in javascript

I have a array of object if property items has same value, return that array object in javascript我有一个 object 的数组,如果属性items具有相同的值,则在 javascript 中返回该数组 object

In the below list, retrieve the array of object having same value in在下面的列表中,检索具有相同值的 object 的数组

items property in javascript items属性在 javascript

note:also dynamic object array, items value may vary, so cannot directly filter with value注意:也是动态的 object 数组,items value 可能会有所不同,所以不能直接用 value 过滤

var list =[
  {id: 1, name: "dev", items: "sales", code: "IN"},
  {id: 2, name: "lena", items: "finance", code: "SG"}
  {id: 3, name: "lisa", items: "sales", code: "AU"},
  {id: 4, name: "mano", items: "marketing", code: "IN"}
]

Expected Result
 {id: 1, name: "dev", items: "sales", code: "IN"}
 {id: 3, name: "lisa", items: "sales", code: "AU"},


var result= list.reduce((m, o) => {
  const found= m.find(e => e.items === o.items);
  if(found){
   m.push(o);
   return m
  }
}, []);

With reduce it's more cumbersome to find out all duplicates.使用 reduce 找出所有重复项会更加麻烦。 You can use closure.您可以使用闭包。

 const list = [ { id: 1, name: "dev", items: "sales", code: "IN" }, { id: 2, name: "lena", items: "finance", code: "SG" }, { id: 3, name: "lisa", items: "sales", code: "AU" }, { id: 4, name: "mano", items: "marketing", code: "IN" }, { id: 5, name: "lisa", items: "gul gul", code: "AU" }, { id: 6, name: "anthony", items: "some", code: "AU" }, { id: 7, name: "mark", items: "gul gul", code: "AU" } ]; const findByItems = (eq) => (arr) => arr.filter( (x, i) => arr.find((y, j) => i,== j && eq(x, y)) ) const duplicatedItems = findByItems((a. b) => a.items === b;items). console.log(duplicatedItems(list))

I would personally group the items in the input by their items values, and then extract the one with the longest group (in your example, finance and marketing would have one item and only sales would have two):我个人会根据项目值对输入中的项目进行分组,然后提取最长的一组(在您的示例中,财务和市场营销将有一个项目,而只有销售会有两个):

var list = [
  {id: 1, name: "dev", items: "sales", code: "IN"},
  {id: 2, name: "lena", items: "finance", code: "SG"},
  {id: 3, name: "lisa", items: "sales", code: "AU"},
  {id: 4, name: "mano", items: "marketing", code: "IN"}
]

const map = {}
for (let item of list) {
  if (map[item.items]) {
    map[item.items].push(item);
  } else {
    map[item.items] = [item]
  }
}

let maxLength = 0, result;
for (let key in map) {
  if (map[key].length > maxLength) {
    maxLength = map[key].length
    result = map[key]
  }
}

console.log(result)

This will return这将返回

[
  { id: 1, name: 'dev', items: 'sales', code: 'IN' },
  { id: 3, name: 'lisa', items: 'sales', code: 'AU' }
]

as you were expecting.如你所料。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取具有相同属性的对象并在数组中具有最大值 - Javascript - how to get objects with the same property and has the max value in array - Javascript Javascript:具有相同值的对象数组的每个元素 - Javascript: each element of a array of objects having same value 如果 JavaScript 中的对象数组中有两个具有相同值的项目,如何删除一个项目? - How to remove ONE item if there are two items having the same value in array of objects in JavaScript? 如何过滤对象数组并检查数组内是否有多个 object 在 Javascript 中具有相同的属性值? - How to filter array of objects and check if more than one object inside the array has the same property value in Javascript? 计算具有相同属性值的数组对象 - Calculation on array objects having same property values 合并与js中的属性具有相同值的对象 - merge objects having same value as property in js 合并共享相同属性值的两个javascript数组对象 - Merging two javascript array objects that share the same property value 如何将数组对象中的第一个值检索到Javascript中的新数组中? - How to retrieve first value from Array objects into a new Array in Javascript? 如何将对象键分配给相同的属性并创建具有名称和值对的对象数组 - how to assign Object keys to a same property and create an array of objects with name and value pair-Javascript Javascript如何将具有相同属性值的两个数组连接在一起? - Javascript how to join two arrays having same property value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM