map function for objects (instead of arrays)

前端 未结 30 2604
无人及你
无人及你 2020-11-22 04:23

I have an object:

myObject = { \'a\': 1, \'b\': 2, \'c\': 3 }

I am looking for a native method, similar to Array.prototype.map

30条回答
  •  深忆病人
    2020-11-22 04:49

    You can convert an object to array simply by using the following:

    You can convert the object values to an array:

    myObject = { 'a': 1, 'b': 2, 'c': 3 };
    
    let valuesArray = Object.values(myObject);
    
    console.log(valuesArray);

    You can convert the object keys to an array:

    myObject = { 'a': 1, 'b': 2, 'c': 3 };
    
    let keysArray = Object.keys(myObject);
    
    console.log(keysArray);

    Now you can do perform normal array operations, including the 'map' function

提交回复
热议问题