1

I'd like to transform the below dataset array into a different format. Currently, each dictionary in the array has a 'count', 'fruit', and 'first name'. I'd like to create a new dictionary for each distinct first name and values that first name has for each 'fruit' type.

For example, see below for some input data

var input_data = [{"count":1,"fruit":"apple","first_name":"abe"},{"count":1,"fruit":"apple","first_name":"bob"},{"count":10,"fruit":"banana","first_name":"bob"},{"count":5,"fruit":"cherry","first_name":"abe"}]

We know for this dataset that the categories are ['apple', 'banana', 'cherry']

var desired_output = 
[{name: 'abe',data:[1,0,5]},
{name: 'bob',data:[1,10,0]}]
5
  • Have you tried groupBy? Commented Aug 8, 2016 at 17:36
  • 1
    I don't see any relation between input_data and data arrays in the desired output? Commented Aug 8, 2016 at 17:40
  • @Bergi - How do you use groupBy? That might be what I'm looking for. Commented Aug 8, 2016 at 21:36
  • @Ozan - the relation is essentially that I want one dictionary for abe and one dictionary for bob, where there is an attribute called 'data' that has a list indicating how many of each fruit he has. So for example, we know Bob above has 1 Apple, 10 Bananas, and 0 Cherries based on the input, so I want the list to be [1,10,0]. Commented Aug 8, 2016 at 21:40
  • @Chris: First, make the categories array from a simple iteration (or something like uniq(map('fruit', data))). Use groupBy with the .first_name property, then map the values to the desired output elements (selecting the name, mapping every item to its index in the categories array). Commented Aug 8, 2016 at 22:02

1 Answer 1

1

You could use a hash table for the name reference and an object for the rigth index for counting.

var input_data = [{"count":1,"fruit":"apple","first_name":"abe"},{"count":1,"fruit":"apple","first_name":"bob"},{"count":10,"fruit":"banana","first_name":"bob"},{"count":5,"fruit":"cherry","first_name":"abe"}],
    categories = ['apple', 'banana', 'cherry'],
    cat = {},
    result = [];

categories.forEach(function (a, i) { cat[a] = i; });
input_data.forEach(function (a) {
    if (!this[a.first_name]) {
        this[a.first_name] = { name: a.first_name, data: categories.map(function () { return 0; }) };
        result.push(this[a.first_name]);
    }
    this[a.first_name].data[cat[a.fruit]] += a.count;
}, Object.create(null));
console.log(result);

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

1 Comment

The only issue here is that I'd like the result to reflect that Bob has 10 bananas and Abe has 5 cherries. For example, the result here generates [{name: 'abe',data:[1,0,1]}, {name: 'bob',data:[1,1,0]}], but I want it to generate [{name: 'abe',data:[1,0,5]}, {name: 'bob',data:[1,10,0]}]

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.