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?
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?
Share Improve this question asked Oct 27, 2017 at 14:19 user7711695user7711695 831 silver badge9 bronze badges 9-
The outer brackets should be curly, not square. Square indicates an array, which doesn't (usually) have named properties like
property: "value"
. Curly is an object, which is more suited to your needs. – Feathercrown Commented Oct 27, 2017 at 14:24 - Oh, ok. In that case maybe I don't need named objects at all. Thanks for letting me know! – user7711695 Commented Oct 27, 2017 at 14:27
- You're wele. And you wouldn't need named objects, per se, more properties of an object with the name as the key and the value as the object. – Feathercrown Commented Oct 27, 2017 at 14:29
- I'm not sure if I quite understand what the difference is... – user7711695 Commented Oct 27, 2017 at 14:44
- I took Stanley Cheung's answer and modified it slightly to get the curly braces on the outside, so instead of var itemArray = [ ], I made it var items = { }... I guess I just don't know what the difference is between named objects and "properties of an object with the name as the key and the value as the object" – user7711695 Commented Oct 27, 2017 at 14:48
2 Answers
Reset to default 4Make 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;
}
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