Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Javascript

ลบเลขศูนย์นำหน้าในอาร์เรย์ JavaScript หรือไม่


ในการลบศูนย์นำหน้าในอาร์เรย์ ให้ใช้แนวคิดของ filter() ต่อไปนี้เป็นข้อมูลที่เรานำเข้าซึ่งเราจะลบศูนย์นำหน้า -

<ก่อน>[10,0,12,0,0])[0,0,0,0,0,0,10,12,0])[12,0,0,1,0,0])

ตัวอย่าง

const removeLeadingZero =input =>input.filter((lastValue => value => lastValue=lastValue || ค่า)(false));console.log(removeLeadingZero([10,0,12,0,0]) );console.log(removeLeadingZero([0,0,0,0,0,0,10,12,0]));console.log(removeLeadingZero([12,0,0,1,0,0]) );

ในการรันโปรแกรมข้างต้น คุณต้องใช้คำสั่งต่อไปนี้ -

โหนด fileName.js

ที่นี่ ชื่อไฟล์ของฉันคือ demo76.js

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

PS C:\Users\Amit\JavaScript-code> โหนด demo76.js[ 10, 0, 12, 0, 0 ][ 10, 12, 0 ][ 12, 0, 0, 1, 0, 0 ]