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?

1
  • 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. Commented Jun 27, 2018 at 4:58

3 Answers 3

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)

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

Comments

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>

4 Comments

What is the meaning of ({ team }) => team.split(',')) I am relatively new to JS
Got it, it's arrow function (shorthand for function without a name) Ref - stackoverflow.com/a/24900924/4050261
@AdarshMadrecha also note that the team string was extracted with the use of Object Destructuring
I used lodash _.split(arr,',') instead of arr.split(',') as lodash takes care of null values.
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

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.