Reputation: 42
I have a question regarding merging two arrays into a JSON object.
I receive an object from an API, the object contains an array of objects (columns) and an array of arrays (data).
I have implemented the following using zipObject from lodash to try and merge the columns and the data and create JSON objects using columns and the data.
function prepareTable() {
let columns = apiData.columns
let array = columns.map(function (obj) {
return obj.title
})
const tableObject = apiData.data.map(datum => {
_.zipObject(array, datum)
})
console.log(tableObject)
}
When I do console.log for apiData.data or columns I can see all the data and also the array as 1 array with 18 elements:
However when I print the tableObject in console it returns undefined.
When I do console.log(typeof datum) for the code above I get 719 object types. I think this is the issue as zipObject receives two arrays as arguments, one for props and one for values.
I do not understand why when I map over an array of arrays I get object as type for each mapped item. I would expect array.
Anyone know where the issue might be and how to fix it?
Upvotes: 0
Views: 168
Reputation: 5946
Simply change to :
const tableObject = apiData.data.map(datum => _.zipObject(array, datum))
Upvotes: 1