简体   繁体   English

如何从对象数组返回匹配的属性值

[英]how to return matched property value from array of object

I have below array, 我有下面的数组,

var abc = [
 {name: 'name1', id: '1', value: 1},
 {name: 'name2', id: '2', value: 3},
 {name: 'name3', id: '3', value: 2},
 {name: 'name4', id: '4', value: 2}
];

i want to return, 我想回来,

var abc = [
 {name: 'name3', id: '3', value: 2},
 {name: 'name4', id: '4', value: 2}
];

because both object has a same value. 因为两个对象的值相同。 How I can achieve this using lodash or javascript. 我如何使用lodash或javascript实现此目的。

You can create an object and its value will an array which will contain only those objects whose value will be same 您可以创建一个对象,其值将是一个数组,该数组仅包含值相同的那些对象

 var abc = [{ name: 'name1', id: '1', value: 1 }, { name: 'name2', id: '2', value: 3 }, { name: 'name3', id: '3', value: 2 }, { name: 'name4', id: '4', value: 2 } ]; let m = abc.reduce(function(acc, curr) { if (!acc.hasOwnProperty(curr.value)) { acc[curr.value] = []; } acc[curr.value].push(curr) return acc; }, {}) Object.keys(m).forEach(function(item) { if (m[item].length > 1) { console.log(m[item]) } }) 

Use filter and findIndex . 使用filterfindIndex

 var abc = [ {name: 'name1', id: '1', value: 1}, {name: 'name2', id: '2', value: 3}, {name: 'name3', id: '3', value: 2}, {name: 'name4', id: '4', value: 2} ]; const res = abc.filter(obj => { return abc.findIndex(obj2 => obj2.id !== obj.id && obj2.value === obj.value) > -1; }); console.log(res); 

You can achieve this using Array.reduce with complexity of O(n) 您可以使用Array.reduce来实现此目标,其复杂度为O(n)

You can create an object/map with key as value property of the object. 您可以使用键作为对象的value属性来创建object/map For each object in array, check for value in object. 对于数组中的每个对象,检查对象中的值。 If value does not exist add it to map. 如果值不存在,则将其添加到地图。 If the value exists ( duplicate ), then add the object to array along with the value stored in map. 如果值存在( 重复 ),则将对象与存储在map中的值一起添加到数组。 You will need to reset the value in map so that you do not end up adding the first value more than once. 您将需要重置map中的值,以免最终添加第一个值不止一次。

 var abc = [{name: 'name1', id: '1', value: 1},{name: 'name2', id: '2', value: 3},{name: 'name3', id: '3', value: 2},{name: 'name4', id: '4', value: 2}]; var obj = {}; var result = abc.reduce((a,c) => { if(obj[c.value]) { if(obj[c.value] !== true) { a.push(obj[c.value]); obj[c.value] = true; } a.push(c); } else obj[c.value] = c; return a; }, []); console.log(result); 

You can group them by the value field using Array.prototype.reduce() 您可以使用Array.prototype.reduce()将它们按value字段分组

Here is how to do so: 这样做的方法如下:

 var abc = [ {name: 'name1', id: '1', value: 1}, {name: 'name2', id: '2', value: 3}, {name: 'name3', id: '3', value: 2}, {name: 'name4', id: '4', value: 2} ]; var reduced = abc.reduce((accumulated, current) => { if(!accumulated[current.value]) { accumulated[current.value] = []; } accumulated[current.value].push(current); return accumulated }, {}) // var value = 1 var value = 2; console.log(reduced[value]) 

Just change the value field to get the required array. 只需更改value字段即可获取所需的数组。

It is interesting that you posted with a lodash tag but got not a single lodash answer. 有趣的是,您张贴了一个lodash标签,但没有一个lodash答案。

I would like to add one since it is more concise by simply using the short form of .countBy and 我想添加一个,因为它更简洁,只需使用.countBy .filter : .filter

 var data = [ {name: 'name1', id: '1', value: 1}, {name: 'name2', id: '2', value: 3}, {name: 'name3', id: '3', value: 2}, {name: 'name4', id: '4', value: 2} ]; var result = _.filter(data, ({value}) => _.countBy(data, 'value')[value] > 1) console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM