This is my code
let array = [
[
['firstName', 'Mary'],
['lastName', 'Jenkins'],
['age', 36],
['gender', 'female'],
],
[
['lastName', 'Kim'],
['age', 40],
['gender', 'female'],
], [
['firstName', 'Joe'],
['age', 42],
['gender', 'male'],
],
]
array.map(function (el) {
return Object.assign({}, el)
})
However this is how it appears in the console
I've tried array.flat()
but did not work as intended
I want the result to look something like this
[[{firstName : 'Mary'}.{lastName : 'jenkins'},..],...]
Do you want a list of objects, or a list of a list of key-value (pair) objects?
let array = [ [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], ] const listOfObjects = array.map(Object.fromEntries); console.log(listOfObjects.map(JSON.stringify).join('\n'));
.as-console-wrapper { top: 0; max-height: 100%;important; }
let array = [ [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], ] const listOfListOfPairs = array.map(items => items.map(item => Object.fromEntries([item]))); console.log(listOfListOfPairs.map(JSON.stringify).join('\n'));
.as-console-wrapper { top: 0; max-height: 100%;important; }
you can use Object.fromEntries
.
in your case something like this would work:
let array = [ [ ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['gender', 'female'], ], [ ['lastName', 'Kim'], ['age', 40], ['gender', 'female'], ], [ ['firstName', 'Joe'], ['age', 42], ['gender', 'male'], ], ] const asObjects = array.map((entries) => Object.fromEntries(entries)) console.log(asObjects)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.