Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically split an array?

I have an array of strings, sort of like this:

["x", "foo", "y", "bar", "baz", "z", "0"]

And I need to split the array for every X, Y, and Z or other special keywords there are.

I've tried to split arrays with [x,y,z].split(y), but I'm pretty sure split() is only for strings.

The keywords (x, y, and z) have to be the first ones in the array. How can I do this?

This is what I'm trying to get:

[
    ["x", "foo"],
    ["y", "bar", "baz"],
    ["z", "0"]
]
like image 331
DexieTheSheep Avatar asked Feb 02 '19 16:02

DexieTheSheep


People also ask

How do you split data in an array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you split an array in C++?

Examples: Input : arr[] = {12, 10, 5, 6, 52, 36} k = 2 Output : arr[] = {5, 6, 52, 36, 12, 10} Explanation : Split from index 2 and first part {12, 10} add to the end . Input : arr[] = {3, 1, 2} k = 1 Output : arr[] = {1, 2, 3} Explanation : Split from index 1 and first part add to the end.

Can arrays grow dynamically?

A dynamic array is an array with a big improvement: automatic resizing. One limitation of arrays is that they're fixed size, meaning you need to specify the number of elements your array will hold ahead of time. A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.

How to split a string into an array of strings?

The Power Automate split function breaks down your string into an array of strings using the delimiter that you defined. And think that the delimiter is as a border.

What is the difference between array_split () and split () methods?

Note: We also have the method split () available but it will not adjust the elements when elements are less in source array for splitting like in example above, array_split () worked properly but split () would fail. The return value of the array_split () method is an array containing each of the split as an array.

How to split a string into arrays in power automate?

In the Power Automate, select the Manually triggered flow. Then click on the Next step. Select the initialize variable action, and then set the variable name, type as string, and value. Now we will split the string into arrays by using characters.

How to split an array into multiple 2-D arrays in JavaScript?

Use the same syntax when splitting 2-D arrays. Use the array_split () method, pass in the array you want to split and the number of splits you want to do. Split the 2-D array into three 2-D arrays. The example above returns three 2-D arrays. Let's look at another example, this time each element in the 2-D arrays contains 3 elements.


1 Answers

You could take an array and a closure over the index for the key strings and push an empty array if a key is found.

var array = ["x", "foo", "y", "bar", "baz", "z", "0"],
    keys = ["x", "y", "z"],
    result = array.reduce((i => (r, s) => {
        if (s === keys[i]) {
            r.push([]);
            i++;
        }
        r[r.length - 1].push(s);
        return r;
    })(0), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 195
Nina Scholz Avatar answered Sep 24 '22 03:09

Nina Scholz