Skip to content

Instantly share code, notes, and snippets.

@Darker
Created February 9, 2018 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Darker/3acd957d0323ad72870e55700e61388c to your computer and use it in GitHub Desktop.
Save Darker/3acd957d0323ad72870e55700e61388c to your computer and use it in GitHub Desktop.
Answer

There's multiple issues:

  • somethingIsTrue ? doSomething(); is not valid in JavaScript. See MDN on ternary operator
  • Frankly, I don't think Array.prototype.reduce is the right choice. Reduce creates something from all array members. I mean you could use it for this, but the code would be unnecesarily obscure. For generic looping, use either map (1:1 conversion of array elements) or forEach

I'd use forEach, or just classic for:

var d_loc = [{'Common Name': "Foo"}, {'Common Name':"Bar"}, {'Common Name': "Foo"}];
const accounts = [];
const result = d_loc.forEach( (userInfo) => { 
    const name = userInfo['Common Name'];
    if(accounts.indexOf(name)<0)
        accounts.push(name) 
    } ,
[] )

console.log(accounts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment