Ahmad Sayadi
Ahmad Sayadi

Reputation: 173

how to convert array in array to array object

I know this has been asked before but I can't seem to find the answer, how to change array in array to object in array ..??

problem this in javaScript.

[
    [
        '2019-08-01',
        'one',
    ],
    [
        '2019-08-02',
        'two',
    ],
    [
        '2019-08-03',
        'tree',
    ]
]


to

[
    {
        'date': '2019-08-01',
        'sort': 'one'
    },
    {
        'date': '2019-08-02',
        'sort': 'two'
    },
    {
        'date': '2019-08-03',
        'sort': 'tree'
    }
    
]

Upvotes: 1

Views: 57

Answers (1)

Ricky Mo
Ricky Mo

Reputation: 7618

Use the Array.map method https://www.w3schools.com/jsref/jsref_map.asp:

let array = [
    [
        '2019-08-01',
        'one',
    ],
    [
        '2019-08-02',
        'two',
    ],
    [
        '2019-08-03',
        'tree',
    ]
];

let result = array.map(x => {return {date:x[0],sort:x[1]}});
console.log(result)

Upvotes: 3

Related Questions