Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

객체 값으로 숫자를 반복하고 출력을 배열로 푸시 - JavaScript?

<시간/>

다음이 개체 값이 있는 숫자라고 가정해 보겠습니다. -

var numberObject = { 2:90 , 6: 98 }

JavaScript에서 Array.from() 사용 -

var fillThePositionValue = Array.from({length: 15}, (value, index) => numberObject[index+ 1] || "novalue")

예시

다음은 개체 값으로 숫자를 반복하는 코드입니다 -

var numberObject = { 2:90 , 6: 98 }
console.log("The actual object is=");
console.log(numberObject);
var fillThePositionValue = Array.from({length: 15}, (value, index) => numberObject[index+ 1] || "novalue")
console.log("After filling the value, the actual object is=");
console.log(fillThePositionValue)

위의 프로그램을 실행하려면 다음 명령을 사용해야 합니다 -

node fileName.js.

여기에서 내 파일 이름은 demo215.js입니다.

출력

출력은 다음과 같습니다 -

PS C:\Users\Amit\JavaScript-code> node demo215.js
The actual object is=
{ '2': 90, '6': 98 }
After filling the value, the actual object is=
[
   'novalue', 90,
   'novalue', 'novalue',
   'novalue', 98,
   'novalue', 'novalue',
   'novalue', 'novalue',
   'novalue', 'novalue',
   'novalue', 'novalue',
   'novalue'
]