1

I have been trying to figure out how to turn an array into an array with objects.

for example i have a json file to start with and the json file looks sorta like this

var data=[{"tasknumber":304030,
       "date":"2012-05-05",
       "operator":"john doe"},
      {"tasknumber":23130,
       "date":"2012-07-07",
       "operator":"john doeeeeeeee"},
       {"tasknumber":233330,
       "date":"2012-08-08",
       "operator":"john doe"}]

so i applied the _.countBy function that is within the underscore.js library and i get an object like this

{"john doe":2,"john doeeeeeeee":1}

ive been trying to figure out how to turn this into an array with objects so it would look something like this but i have failed in every attempt and i dont know were to start

[{operator:"john doe",
 count: 2},
{operator: "john doeeeeeeee",
count:1}]

i have tried a few things but all i get is tragedy and everything breaks, does anyone know if there are any librarys or anything that could help with this sort of thing?

1
  • 2
    {"john doe":2,"john doeeeeeeee":1} is object not an array Commented Dec 22, 2016 at 0:14

3 Answers 3

3

Given the object (not array) {"john doe":2,"john doeeeeeeee":1} as input you can get your desired output like this:

var input = {"john doe":2,"john doeeeeeeee":1};

var output = Object.keys(input).map(function(k) {
  return {
    operator: k,
    count: input[k]
  };
});

console.log(output);

Or with ES6 arrow function syntax:

var input = {"john doe":2,"john doeeeeeeee":1};

var output = Object.keys(input).map((k) => ({ operator: k, count: input[k] }) );

console.log(output);

(Note that Underscore probably provides an even shorter way to do this, but I'm not familiar with Underscore so I've just given a plain JS solution.)

Further reading:

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

4 Comments

when i run this my count returns an undefined
Well the "Run..." button shows that my code works for the {"john doe":2,"john doeeeeeeee":1} object you showed in your question. I can't tell what's wrong when you tried it without seeing exactly what you did.
Thank you very much for the help, imma see if i can fix this bug but u helped me drastically
You're welcome. I've added some doco links to my answer in case you want to read up more on the functions and syntax in my code. Merry Christmas.
0

Given your initial data array, you can just run this:

var data=[{"tasknumber":304030,
"date":"2012-05-05",
"operator":"john doe"},
{"tasknumber":23130,
"date":"2012-07-07",
"operator":"john doeeeeeeee"},
{"tasknumber":233330,
"date":"2012-08-08",
"operator":"john doe"}];

Function definition

const count = data => {
  // get data in format like _.countBy
  const o = data.map(x => x.operator).reduce((acc, cur) => { acc[cur] ? acc[cur] += 1 : acc[cur] = 1; return acc; }, {});
  // transform object into array of object
  return Object.keys(o).map(operator => ({operator, count: o[operator]}));
};

Test it by producing output

console.log(count(data));

Comments

0

Here is an untested underscore approach that takes your initial values as loaded from the JSON file and converts directly into your desired output format:

_.chain(input)
 .groupBy(function(entry) { return entry.operator })
 .map(function(entries, operator) { 
        return {
           operator: operator,
           count: entries.length
        }   
      })
 .value();

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.