1

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'
    }
    
]

2
  • the answer is probably where it's been asked before Commented Jul 26, 2019 at 3:05
  • are date, sort hardcode or dynamic Commented Jul 26, 2019 at 3:15

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.