Split array into arrays of matching values

Related searches

I have an array of values similar to:

["100", "330", "22", "100", "4.7", "0.01", "0.01", "330", "0.01", "100", "47", "22", "100", "0.01"]

Using JavaScript I would like to split this array into a number of arrays where only matching strings are in those arrays like this:

["100", "100", "100"]
["330", "330"]
["22", "22"]
["0.01", "0.01", "0.01", "0.01"]

Any help with this would be much appreciated! Thanks!

Split an array into 2 arrays based on conditions : javascript, Using ES6 or Rxjs, would it be possible to split an array into 2 arrays based on conditions and without doing any state management or temp variables … splitByChar (separator, s) Splits a string into substrings separated by a specified character. It uses a constant string separator which consisting of exactly one character. Returns an array of selected substrings. Empty substrings may be selected if the separator occurs at the beginning or end of the string, or if there are multiple consecutive separators.

Use reduce method and Object.values

   //Go thru each value in `arr` array and convert into object with 
   //keys as unique (value in array) and values array of value repetitions.

     "100": ["100", "100"],
     "22": ["22"],

   //Use the Object.values to get the Values as array of arrays

const arr = ["100", "330", "22", "100", "4.7", "0.01", "0.01", 
             "330", "0.01", "100", "47", "22", "100","0.01"];

const updated = Object.values(
    (acc, curr) => (
      (acc[curr] = curr in acc ? [...acc[curr], curr] : [curr]), acc


Split an array into two equal Sum subarrays, Given an array of integers greater than zero, find if it is possible to split it in two subarrays (without reordering the elements), such that the sum� numpy.array_split () function The array_split () function split an given array into multiple sub-arrays. The only difference between these functions is that array_split allows indices_or_sections to be an integer that does not equally divide the axis.

Here's another take on using reduce to group by the values.

It reduces to an object, which is then transformed to an array of arrays.

let valueArray = ["100", "330", "22", "100", "4.7", "0.01", "0.01", "330", "0.01", "100", "47", "22", "100", "0.01"];

let groupedByValue = Object.values(valueArray
  .reduce((acc, x)=>{
   	  let arr = acc[x] || [];
   	  acc[x] = arr;
   	  return acc;

Array methods, But for arrays we usually want the rest of elements to shift and occupy The method arr.slice is much simpler than similar-looking arr.splice . The syntax is similar to find , but filter returns an array of all matching elements:. I want to split this array into multiple arrays based on the 2nd value in the array (3.0, 3.0, 3.01.0,1.0,10). Every time the 2nd value changes, I want a new array, so basically each new array has the same 2nd value. I've looked this up on Stack Overflow and know of the command . np.split(array, number)

Also it can be done like this:

const {log} = console;
const arr = ["100", "330", "22", "100", "4.7", "0.01", "0.01", "330", "0.01", "100", "47", "22", "100", "0.01"];
log([...new Set(arr)].map( x => arr.filter( y => y === x )));

Indexed collections, If you wish to initialize an array with a single element, and the array is the return value of RegExp.exec() , String.match() , and String.split() . Split the array into equal sum parts according to given conditions. Given an integer array arr [], the task is to check if the input array can be split in two sub-arrays such that: Sum of both the sub-arrays is equal. All the elements which are divisible by 5 should be in the same group.

Here's an example of what you want using Set.

const data = ["100", "330", "22", "100", "4.7", "0.01", "0.01", "330", "0.01", "100", "47", "22", "100", "0.01"];

const unique = new Set(data);
for(let num of unique) {
  console.log(data.filter(d => d == num));

split array into chunks Code Example, Get code examples like "split array into chunks" instantly right from your google elements in javascript � how to split an array to multiple arrays as sets typescript babel-loader jsx SyntaxError: Unexpected token [duplicate]� In this post, we will see how to split array into two parts in Java. If the array contains an odd number of items, the extra item should be part of the first array. 1. Naive. Naive solution would to create two new arrays and assign elements from first half of the source array to the first array and elements from second half of the source array

split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression (pattern), a group of characters or on undefined value etc..

This code changes the two arrays into a dictionary where the key of the dictionary is the index of the array and the value of the dictionary is the pair of array values. It then uses the GroupBy LINQ expression to group the pairs. The last For/Each statement then prints the counts and indexes showing the matching pairs.

Split() function splits the input string into the multiple substrings based on the delimiters, and it returns the array, and the array contains each element of the input string. By default, the function splits the string based on the whitespace characters like space, tabs, and line-breaks. If you want to split the string based on the specific character, then you must specify the character in the second argument. Following is the syntax of the .split() function. For example, we want to split

  • What have you tried so far?
  • I haven't tried anything as of yet, I'm still relatively new to JavaScript and don't know where to start
  • This part seems not valid: "0.01, "330", 0.01"
  • Thank you, I like this solution! Could you also add a few comments for me to help me understand and learn the code I'm using :)
  • Spreading out the accumulator is a bad practice. Consider returning the accumulator instead.
  • @Siva could you just explain the code to me so I can get a better understanding of it? I like to understand the code I'm using!
  • Sure @Alex i will add details soon.
  • @AlexMorrison, updated the answer with comments. Hope that helps.
  • @SivaKondapiVenkata You have made it a lot more complicated than it has to be