0

In my JS project, I am using Lodash library to Extract property, split array, get unique values.

  var taskobj = [
      {'taskno':'a', 'team':'1,2'},
      {'taskno':'b', 'team':'3,4'},
      {'taskno':'c', 'team':'2,4'},
    ];

 //Looping through the object to convert string to array
  _.forEach(taskobj, function(value, key) {
    taskobj[key].team = _.split(taskobj[key].team,',');
  });

// using _.map to extract team and return array
// using _.flatten to flatten array
// using _.uniq to get unique values from flattned array.

return _.uniq(_.flatten(_.map(taskobj,'team')));
// logs - [1,2,3,4]

Is this the most efficient way to achieve this?

Adarsh Madrecha
  • 6,364
  • 11
  • 69
  • 117
  • `var teams = []; taskobj.forEach(o => teams.push(...o.team.split(','))); _.uniq(teams);` You can modify this to add a condition to check if element exists in the `teams` array before pushing and remove `uniq`. – Tushar Jun 27 '18 at 04:58

3 Answers3

3

you can use reduce and start with a new Set() and add the values of team every time ( then convert it back to an array with the spread operator )

var taskobj = [
  {'taskno':'a', 'team':'1,2'},
  {'taskno':'b', 'team':'3,4'},
  {'taskno':'c', 'team':'2,4'},
];

var result = [...taskobj.reduce((acc, {team}) => {
  team.split(',').forEach(e => acc.add(e))
  return acc
}, new Set())]

console.log(result)
Taki
  • 17,320
  • 4
  • 26
  • 47
1

Use simpler version

try this

var teams = [];
var taskobj = [
      {'taskno':'a', 'team':'1,2'},
      {'taskno':'b', 'team':'3,4'},
      {'taskno':'c', 'team':'2,4'},
    ];

taskobj.map(obj => {
  var teamSplit = obj.team.split(',');
  teams = [...teams, ...teamSplit];
})

var uniqTeams = _.uniq(teams);
console.log('teams', teams);
console.log('uniqTeams', uniqTeams)

JsBin link http://jsbin.com/bedawatira/edit?js,console

Piyush Patel
  • 1,212
  • 15
  • 19
1

This can be achieved by using lodash#flatMap with an iteratee that splits the team string into an array, which is then flattened by the mentioned function and then use lodash#uniq to get the final result.

var result = _.uniq(_.flatMap(taskobj, ({ team }) => team.split(',')));

var taskobj = [
    {'taskno':'a', 'team':'1,2'},
    {'taskno':'b', 'team':'3,4'},
    {'taskno':'c', 'team':'2,4'},
];

var result = _.uniq(_.flatMap(taskobj, ({ team }) => team.split(',')));

console.log(result);
.as-console-wrapper{min-height:100%;top:0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
ryeballar
  • 29,658
  • 10
  • 65
  • 74
  • What is the meaning of `({ team }) => team.split(','))` I am relatively new to JS – Adarsh Madrecha Jun 27 '18 at 06:00
  • Got it, it's arrow function (shorthand for function without a name) Ref - https://stackoverflow.com/a/24900924/4050261 – Adarsh Madrecha Jun 27 '18 at 06:26
  • 1
    @AdarshMadrecha also note that the `team` string was extracted with the use of [Object Destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring) – ryeballar Jun 27 '18 at 11:50
  • I used lodash `_.split(arr,',')` instead of `arr.split(',')` as lodash takes care of null values. – Adarsh Madrecha Jun 27 '18 at 14:50