0

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.

3
  • let filtered = abc.filter(element => element.value === 2); Commented Sep 6, 2018 at 12:59
  • If there are 3 objects with same value, do you want to return all 3 values? Also, can there be more than 1 combination of same value? Commented Sep 6, 2018 at 13:02
  • Do you want to search for a specific value, or find duplicates in the given array? Commented Sep 6, 2018 at 13:07

5 Answers 5

3

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])

  }

})

Sign up to request clarification or add additional context in comments.

Comments

2

Use filter and findIndex.

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);

Comments

1

You can achieve this using Array.reduce with complexity of O(n)

You can create an object/map with key as value property of the object. 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. You will need to reset the value in map so that you do not end up adding the first value more than once.

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);

Comments

0

You can group them by the value field using Array.prototype.reduce()

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.

Comments

0

It is interesting that you posted with a lodash tag but got not a single lodash answer.

I would like to add one since it is more concise by simply using the short form of .countBy and .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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.