Monday, 20 April 2020

Combining multiple JSON objects into one.

Hello,I have a project where I'm using Node to read a csv and converting it to JSON. The CSV is a list of pictures and attributes.The wrinkle is that each attribute gets a new line in the CSV so depending on things one picture may have several lines in the CSV and I want to combine things so that I have one object per picture and then put the attributes into a smaller object.Right now everything I'm trying just pushes out every attribute except the last one so I'd like to know what I could do that matches the file names of the pictures and then creates an array of the attributes inside the new object.Right now:​var fs = require('fs'); var arr1 = [{ "path,": "picture_1.jpg", "attribute": "genderf", "value": "0.024716798" }]; var arr2 = [{ "path,": "picture_1.jpg", "attribute": "genderm", "value": "0.975283206" }] let result = arr1.map((e) => { for(let element of arr2){ if(e.a == element.a) Object.assign(e, element); } return e; }); console.log(result); fs.writeFileSync('result.json', JSON.stringify(result)); With the result being:[{"path,":"picture_1.jpg","attribute":"genderm","value":"0.975283206"}]But I want:[{"path,":"picture_1.jpg","attribute":["genderm", "genderf"],value":["0.975283206", "0.024716798" ]}]

Submitted April 20, 2020 at 03:23PM by madmoneymcgee

No comments:

Post a Comment