2 diseases

Title: 1-12 presence of a company sales in a month inside the object, as follows: {1: 222, 2: 123, 5: 888}, the data processing request as a structure: [222, 123, null , null , 888 , null , null , null , null , null , null , null ].

@ Allay:
the let obj
= {. 1: 222, 2: 123,. 5: 888 }; const Result = Array.from ({length: 12 is }) Map (. (_, Index) => obj [index +. 1 ] || null ); the console.log (Result);

An array of methods:

from()

Array.from () method of a class is to convert the array can traverse the object or objects into a real array. 

To a class object into an array of real array, must meet the following conditions:

   1 , such an array of objects must have attributes length, specifies the length of the array. If there is no length property, the array is converted to an empty array. 

  2, the class property name is an array of objects must be numeric or string type digital

 

 

map()

map () method returns a new array, the array element calls a function of the value of the original processing array elements. 

Returns a new array, the array element calls a function of the value of the original processing array elements. 

Syntax: Array.map ( function (currentValue, index, arr), thisValue) 

function (currentValue, index, arr)

 

currentValue have to. The current value of the element
index Optional. The current index value of the element
arr Optional. Current array object element belongs

Guess you like

Origin www.cnblogs.com/anbozhu7/p/11271685.html