How to convert an object to an array?

  • 0
    Is it possible for such an object {name: 'Ivan', surname: 'Ivanov'} to convert exactly such an array [name: 'Ivan', surname: 'Ivanov']. If so how
    JavaScript Annabelle Bishop, Dec 1, 2020

  • 3 Answers
  • 0
    formally, this is possible, an array is also an object, but why do that is a mystery.



    const obj = {name: 'Иван', surname: 'Иванов'}
    const arr = Array(0);
    Object.keys(obj).forEach(key => arr[key] = obj[key]);
    console.log(arr);



    Anonymous

  • 0
    an array has numeric indices, but an object can also be accessed as an array: obj ['name']
    Anonymous

  • 0
    This conversion cannot be performed. You can only do something like this:

    var obj = {name: 'Иван', surname: 'Иванов'};
    var arr = [];
    for (let key in obj) {
    let temp = key + ': ' + obj[key];
    arr.push(temp);
    }
    console.log(arr);
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!