As a programmer and a developer, we constantly try to figure out different approaches for a specific problem. Trying to figure out the optimal solution is what I have always looking for as a developer. And it is always good to find different solutions to a specific problem so that we can choose the one that is easy to understand and optimized.
How To Create an array from a given string
split() method(before es6, not es5 as the split was introduced in the ES5 version). But after the arrival of ES6 and the above specifications, several new methods are also introduced features like,
Object.assign()spread operator and
Array.from. We use these modern methods for manipulating the strings and arrays.
Using the Split Method
We use the
split() method to convert the string into an array. When we apply the method to a string, then it returns us back with the substring in an array. The
split() method takes delimiter as its parameter and then on the basis of the argument it receives, it then splits the given string. Let us look at an example below
let sentence = "Some Cool Text Here"; let toArray = sentence.split(" "); //output = ['Some','Cool','Text','Here']
Code with Explanation:
In the above example, we pass
" " as a delimiter argument and the string we receive will be an array. If we change the delimiter with a comma, then we will get a comma separate value
Using The Spread Operator
We can create an array of substrings using the spread operator. The spread operator will simply treat each value as different than that of counting it as a single word. However, you can find more about the spread operator in the MDN Docs for more specific details.
let sentence = "Some,Cool,Text,Here"; console.log(...sentence) //output = ['S', 'o', 'm', 'e', ' ', 'C', 'o', 'o', 'l', ' ', 'T', 'e', 'x', 't', ' ', 'H', 'e', 'r', 'e']
Frequently Asked Questions
In simple terms Pagination means you move from one page to another page either by clicking the Buttons or Links attached to it. The items can be as First Page, Next Page, Previous Page, and the Last Page buttons or links depending upon the site you visit. To know more Click Here.