1

I'm trying to write a function which would take in the following array,

var data = [
            {
                name: 'Josh',
                description: 'some data about this guy',
                sortOrder: 'Gold'
            }, 
            {
                name: 'Bill',
                description: 'some data about this guy',
                sortOrder: 'Platinum'
            },
            {
                name: 'Gary',
                description: 'some data about this guy',
                sortOrder: 'Platinum'
            },
            {
                name: 'Phillip',
                description: 'some data about this guy',
                sortOrder: 'Platinum'
            },
            {
                name: 'Bob',
                description: 'some data about this guy',
                sortOrder: 'Gold'
            },
            {
                name: 'Barry',
                description: 'some data about this guy',
                sortOrder: 'Gold'
            },
            {
                name: 'Joe',
                description: 'some data about this guy',
                sortOrder: 'Gold'
            },
            {
                name: 'Ed',
                description: 'some data about this guy',
                sortOrder: 'Silver'
            },
            {
                name: 'Baxter',
                description: 'some data about this guy',
                sortOrder: 'Platinum'
            },
            {
                name: 'Patrick',
                description: 'some data about this guy',
                sortOrder: 'Silver'
            },
            {
                name: 'Stepehn',
                description: 'some data about this guy',
                sortOrder: 'Silver'
            }
];

and give me an array which looked like this:

[Platinum, Gold, Silver] <-- The order doesn't matter

So it would check all sortOrder values in the array and return an array containing one instance of each unique sortOrder.

Anything I've written so far keeps giving me multiple instances of each sortOrder.... Could anyone give me a hand?

I have access to the underscore.js library if that can be of any help.

1

7 Answers 7

3

This should work great (clean js):

var orders = [];

for (var i = 0; i < data.length; i++){

    if (data[i].sortOrder && orders.indexOf(data[i].sortOrder) == -1){
        orders.push(data[i].sortOrder);
    }

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

4 Comments

What is preventing multiple adds of the same value here? Repeating if ('foo' && !orders['foo']){ orders.push('foo'); } keeps adding to orders
@PaulS. it shouldn't. Can you give me an example?
@YavorIvanov—orders[data[i].sortOrder] is checking orders for properties named Gold, Platinum, etc. but they are values within the array, not names of properties. This just creates an array of all the sortOrder values, duplicates included. Perhaps you missed the part "…return an array containing one instance of each unique sortOrder.".
@PaulS. OMG :D I'm kinda ashamed. You're right thanks :D I'm really sleepy
2

Try this

var result = _.chain(data)
  .pluck('sortOrder')
  .uniq()
  .value();

console.log(result);

Example

Comments

2

With underscore :

console.log(_.chain(data).pluck('sortOrder').uniq().value());

Comments

1

As the values are String we can go via an Object in vanilla JavaScript

var a = (function (arr) {
    var i, o = {};

    for (i = 0; i < arr.length; ++i)
        o[arr[i].sortOrder] = true;

    return Object.keys(o);
}(data));

Comments

1

Without using any other library:

var results = [];
for(var i = 0; i < data.length; ++i){
    if(results.indexOf(data[i].sortOrder) < 0){
      results.push(data[i].sortOrder);
    }
}

The results variable would contain the list you want.

2 Comments

You don't need a special case for .length === 0, indexOf will give -1 still
True. Not sure what I was thinking when I wrote the first part. Edited to take that out.
0

Lots of plain JS offerings, which are always good to compare with libraries. Consider using ES5's reduce:

var result = data.reduce(function(acc, val) {
  if (acc.indexOf(val.sortOrder) == -1) acc.push(val.sortOrder);
  return acc;
},[]);

Comments

0

I feel like I'd use a Set to ensure the sortOrder stays single-entry. You can use the spread operator to place it back into an Array if that's the data structure you need. You don't have to write a new function or a lot of logic to check if an entry exists this way, though you could wrap the whole thing in a function if it is something you need to reuse a lot.

const result = [...new Set(data.map(({ sortOrder }) => sortOrder))];

Or as a function:

const getAllSortOrders = (data) => [...new Set(data.map(({ sortOrder }) => sortOrder))];

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.