Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add names to an array of unnamed JSON objects?

So I'm currently using csvtojson in order to convert a csv file to, well, json, and my code is returning an array of unnamed objects. However, I want these objects to be named. More specifically, I want to use the values from the first column in order to name the objects.

My CSV file looks like this:

First Name, Restaurant, Food Name, Comment, Price

Andrew, Clucky's Chicken, Chickenator, This sandwich is awesome, $9.99

Michelle, Bytes, Big Burger, Burger was too well done, $12.99

Cam, Candyland, Yummy Gummies, Good price for bulk candy, $1.75

I'm using this code and running it in node:

// require the csvtojson converter class
var Converter = require("csvtojson").Converter;
//create a new converter object
var converter = new Converter({});

//call the fromFile function which takes in the path to the csv file, as well as a callback function
converter.fromFile("./restaurants.csv", function(err,result){
  // if an error has occurred, then handle it
  if(err){
    console.log("An error has occurred");
    console.log(err);
  }
  // create a variable called json and store the result of the conversion
  var json = result;

  // log our json to verify it has worked
  console.log(json);
});

Which returns:

[ { 'First Name': 'Andrew',
    'Restaurant': 'Clucky's Chicken', 
    'Food Name': 'Chickenator',
    'Comment': 'This sandwich is awesome',
    'Price': '$9.99' },
  { 'First Name': 'Michelle',
    'Restaurant': 'Bytes', 
    'Food Name': 'Big Burger',
    'Comment': 'Burger was too well done',
    'Price': '$12.99' },
  { 'First Name': 'Cam',
    'Restaurant': 'Candyland', 
    'Food Name': 'Yummy Gummies',
    'Comment': 'Good price for bulk candy',
    'Price': '$1.75' } ]

But I would like it to return something more along the lines of:

[ Andrew : { 'Restaurant': 'Clucky's Chicken', 
             'Food Name': 'Chickenator',
             'Comment': 'This sandwich is awesome',
             'Price': '$9.99' },
  Michelle : { 'Restaurant': 'Bytes', 
               'Food Name': 'Big Burger',
               'Comment': 'Burger was too well done',
               'Price': '$12.99' },
  Cam : { 'Restaurant': 'Candyland', 
          'Food Name': 'Yummy Gummies',
          'Comment': 'Good price for bulk candy',
          'Price': '$1.75' } ]

Anybody have any suggestions on how I can do this?

like image 661
user7711695 Avatar asked Oct 27 '17 14:10

user7711695


2 Answers

Make a custom function (since you want to convert array into a map).

function arrayToMap(array) {
  var map = {};

  array.map(
    (element) => {
      var firstName = element['First Name'];
      delete element['First Name'];
      map[firstName] = element;          
    }    
  );
  return map;
}
like image 166
Ante Jablan Adamović Avatar answered Oct 02 '22 11:10

Ante Jablan Adamović


Create a new array itemArray, then loop for all item in array and push it to itemArray

var itemArray = []
a.map(item => {
    firstName = item["First Name"];
    delete item["First Name"];
    itemArray[firstName] = item;
})
console.log(itemArray); // <<<< you get the result here
like image 40
Stanley Cheung Avatar answered Oct 02 '22 12:10

Stanley Cheung