How to split each element of an array of strings into different categories using Node.js? Last Updated : 04 Apr, 2023 Comments Improve Suggest changes Like Article Like Report The task is to split each element of a string array into different categories. Example: Input Array : const input = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stw', 'xyz'] Category Array : const categories = ['a', 'b', 'c'] Split Size : const split_size = 3 Output:[ a: [ 'abc', 'def', 'ghi' ], b: [ 'jkl', 'mno', 'pqr' ], c: [ 'stw', 'xyz' ] ] Approach: Initialize an object that will store the categories in it and also initialize a variable index that will keep the index of the categories.Start traversing the array and split it according to the split size.Assign the new split array to the given category and increment the index. The code for the above approach is given below. JavaScript function split_array(array, categories, split_size) { // Initialize empty array const chunks = []; // Initialize index equal to zero to keep track of categories const index = 0; // Traverse the array according to the split size for (let i = 0; i < array.length; i += split_size) { // Slicing the split_size array from i let temp = array.slice(i, i + split_size); // Assigning the sliced array to the category chunks[categories[index]] = temp; // Increment index to keep track of categories index++; } // return the final array return chunks; } //input array const input = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stw', 'xyz'] //categories array const categories = ['a', 'b', 'c'] const split_size = 3; //split size //calling the function const output = split_array(input, categories, split_size); console.log(output) //printing the array Output: [ a: ["abc", "def", "ghi"] b: ["jkl", "mno", "pqr"] c: ["stw", "xyz"] ] Comment More infoAdvertise with us Next Article How to split each element of an array of strings into different categories using Node.js? A adityapande88 Follow Improve Article Tags : Web Technologies Node.js NodeJS-Questions Similar Reads How to Split an Array of Strings into Chunks of Equal Length in TypeScript ? In TypeScript, to split an array of strings into chunks of equal length we need to divide the array into the smaller subarrays, in which each contains the number of elements based on the chunk size. These are the following approaches:Table of Content Using for LoopUsing Array.from() methodUsing redu 3 min read How to create a string by joining the elements of an array in JavaScript ? Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. There are two methods by which we can create a string by joining the elements of an array: Table of Content Using arr.join() MethodUsing 2 min read How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met 2 min read How To Split a String Into Segments Of n Characters in JavaScript? When working with JavaScript, you might encounter a situation where you need to break a string into smaller segments of equal lengths. This could be useful when formatting a serial number, creating chunks of data for processing, or splitting a long string into manageable pieces. In this article, we 6 min read How to convert a DOM nodelist to an array using JavaScript ? NodeList objects are the collection of nodes, usually returned by properties such as Node.childNodes and methods such as document.querySelectorAll(). Although NodeList is not an actual Array but it is possible to iterate over it with the help of forEach() method. NodeList can also be converted into 2 min read Like