Found 10483 Articles for Web Development

Converting string to an array in JavaScript

Nikhilesh Aleti
Updated on 02-Sep-2023 15:45:46

48K+ Views

The task we need to accomplish is converting an input string to an array in JavaScript. Whenever we try to break a string word into characters or a sentence into words – splitting into arrays, we have some built-in methods to convert string to an array. In this article, we will discuss how to convert the string to an array using different methods in JavaScript. Using the split() method This method will split the string to an array of substrings. This will return the output in a new array and will not change the existing string. The split() method accepts ... Read More

Picking out uniques from an array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:37:34

130 Views

Suppose we have an array that contains duplicate elements like this −const arr = [1, 1, 2, 2, 3, 4, 4, 5];We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 1, 2, 2, 3, 4, 4, 5]; const extractUnique = arr => {    const res = [];    for(let i = 0; i < arr.length; i++){   ... Read More

Non-composite numbers sum in an array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:36:18

160 Views

We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % ... Read More

Inverting slashes in a string in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:34:40

339 Views

We are required to write a JavaScript function that takes in a string that may contain some backward slashes.And the function should return a new string where all the backslashes with forward slashes.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th/s str/ng /conta/ns some/ forward slas/hes'; const invertSlashes = str => {    let res = '';    for(let i = 0; i < str.length; i++){       if(str[i] !== '/'){          res += str[i];          continue;       };       ... Read More

Finding sort order of string in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:33:23

177 Views

We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example:isSorted('adefgjmxz') // true isSorted('zxmfdba') // true isSorted('dsfdsfva') // falseTherefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); const isStringSorted = (str = '') => {    if(str.length < 2){       return true;    };    let res = ''    for(let i = 0; i < str.length-1; i++){       if(findDiff(str[i+1], str[i]) > 0){         ... Read More

Pushing false objects to bottom in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:30:28

185 Views

Suppose we have an array of objects like this −const array = [    {key: 'a', value: false},    {key: 'a', value: 100},    {key: 'a', value: null},    {key: 'a', value: 23} ];We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [    {key: 'a', value: false},    {key: 'a', ... Read More

Frequency of vowels and consonants in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:27:02

258 Views

We are required to write a JavaScript function that takes in a string which contains English alphabets. The function should return an object containing the count of vowels and consonants in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is a sample string, will be used to collect some data'; const countAlpha = str => {    return str.split('').reduce((acc, val) => {       const legend = 'aeiou';       let { vowels, consonants } = acc;       if(val.toLowerCase() === val.toUpperCase()){         ... Read More

Finding upper elements in array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:24:44

136 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => {    const res = ... Read More

Maps in JavaScript takes keys and values array and maps the values to the corresponding keys

AmitDiwan
Updated on 21-Oct-2020 12:22:55

169 Views

Suppose we have two arrays −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys.Therefore, the output should look like −const map = {    0 => 'first',    4 => 'second',    2 => 'third',    3 => 'fourth',    1 => 'fifth' };Therefore, let’s write the code for this function −ExampleThe code for this will be −const keys = [0, 4, 2, 3, 1]; const values = ["first", ... Read More

Converting array of objects to an object in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:22:03

476 Views

The given there is an array and the task is to convert an array of objects to an object. Input-Output Scenario Let’s look into an input-output scenario of converting the array objects into an object. Consider there is an array having objects in it. Now, we need to convert those array objects into a single JSON object. Const Arr = [ {1:'one'}, {2:'two'} ]; Output = { "1":"one", "2":"two" }; //Object Using Object.assign() The ... Read More

Advertisements