0

I have a string of values

"000111111122222223333333444455556666"

How could I use a loop to produce one array for index values from 0 to 3 (create an array of [000] and then another array of index values from 3 to 10, 10 to 17, 17 to 24, producing eg. [1111111, 2222222, 333333] and then another loop to produce an array of index values from 24 to 28, 28 to 32, 32 to 36, producing eg. [4444, 5555, 6666])?

So in total 3 different arrays have been created using three different for loops.

array1 = [000]
array2 = [1111111, 2222222, 333333]
array3 = [4444, 5555, 6666]
8
  • if I understand correctly you want to separate value by their quantity in the string ? What would be the expected output from 0112, 01100 and 0110 Commented Aug 20, 2018 at 12:50
  • Please provide your desired output, from what you've written it's a bit hard to tell what you want for the final outcome Commented Aug 20, 2018 at 12:54
  • 3
    what is with your last question? stackoverflow.com/questions/51910212/… Commented Aug 20, 2018 at 12:56
  • @NinaScholz even if it is REALLY close it is not exactly the same, this time he want to output differents array, maybe with not defined size for each parts Commented Aug 20, 2018 at 12:57
  • and btw, no attempt. Commented Aug 20, 2018 at 12:59

5 Answers 5

0

You may wish to try something line this (only a schematic solution!):

var l_Input = "000111111122222223333333444455556666" ;

var l_Array_1 = [] ;
var l_Array_2 = [] ;
var l_Array_3 = [] ;


var l_One_Char ;

for (var i = 0 ; i < l_Input.length ; i++) {

    l_One_Char = l_Input.substring(i,i) ;

    if (i < 3) {
        l_Array_1.push(l_One_Char) ;
        continue ;
    }

    if (i >= 3 && i < 10) {
        l_Array_2.push(l_One_Char) ;
        continue ;
    }
    :
    :

}

Sign up to request clarification or add additional context in comments.

Comments

0

I think this would work.

const str = '000111111122222223333333444455556666';

function makeArr(str, item) {
    let firstIndex = str.indexOf(item);
    let lastIndex = str.lastIndexOf(item) + 1;
    return [ str.substring(firstIndex, lastIndex) ];
}

const first = makeArr(str, 0);
const second = [].concat(makeArr(str, 1))
                 .concat(makeArr(str, 2))
                 .concat(makeArr(str, 3));
const third = [].concat(makeArr(str, 4))
                .concat(makeArr(str, 3))
                .concat(makeArr(str, 3));

Comments

0

You could map the sub strings.

var str = '000111111122222223333333444455556666',
    parts = [[3], [7, 7, 7], [4, 4, 4]],
    result = parts.map((i => a => a.map(l => str.slice(i, i += l)))(0));

console.log(result);

Comments

0
function split(string, start, end) {
    var result = [],
        substring = string[start],
        split;

    for (var i = start + 1; i < end; i++) {
        var char = string[i];
        if (char === substring[0])
            substring += char;
        else {
            result.push(substring);
            substring = char;
        }
    }
    result.push(substring);
    return result;
}

split("00011122",0,8)

["000", "111", "22"]

Comments

0

To do this dynamically, you can use .split() and .map() methods to make an array from your string then group this array items by value.

This is how should be our code:

const str = "000111111122222223333333444455556666";

var groupArrayByValues = function(arr) {
  return arr.reduce(function(a, x) {
    (a[x] = a[x] || []).push(x);
    return a;
  }, []);
};

var arr = str.split("").map(v => +v);
var result = groupArrayByValues(arr);

This will give you an array of separate arrays with similar values each.

Demo:

const str = "000111111122222223333333444455556666";

var groupArrayByValues = function(arr) {
  return arr.reduce(function(a, x) {
    (a[x] = a[x] || []).push(x);
    return a;
  }, []);
};

var arr = str.split("").map(v => +v);
var result = groupArrayByValues(arr);


console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.