JavaScript generates simple arithmetic differential series and javascript arithmetic differential Series

Source: Internet
Author: User

JavaScript generates simple arithmetic differential series and javascript arithmetic differential Series

The question is very simple. The simplest way is to use the for loop.

let arr = []for (let i = 0; i < b - a + 1; i++) { arr.push(i + a)}return arr

Advanced

When I think about it later, I feel that the previous method is a bit stupid. Then I come up with these methods again.

Array vacancy

Join () and toString () will regard the blank space as undefined (string form ):

// Splice> split> mapArray (B-a + 1 ). join (''). split (''). map (e, I) => a + I) // convert string> split> mapArray (B-a + 1 ). toString (). split (','). map (e, I) => a + I)

Use the Array. from method to implement:

// Convert an empty Array to an Array. from (Array (B-a + 1 )). map (e, I) => a + I) // convert objects similar to arrays. from ({length: B-a + 1 }). map (e, I) => a + I) Array. from ({length: B-a + 1}, (e, I) => a + I)

The extension operators of ES6 can help us complete this task more conveniently.

[... Array (B-a + 1)]. the map (e, I) => a + I) fill (), entries (), and keys () methods do not ignore the empty Array (B-a + 1 ). fill (''). map (e, I) => a + I )[... array (B-a + 1 ). entries ()]. map (e => e [0] + )[... array (B-a + 1 ). keys ()]. map (e => e +)

There are other ways to do this, such as findIndex (), find (),...

Summary

The above section describes how to generate a simple differential series using JavaScript, which is helpful to you. If you have any questions, please leave a message and the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.