How to get all substrings (contiguous subsequences) of my JavaScript array?
Clash Royale CLAN TAG#URR8PPP
up vote
15
down vote
favorite
My task is to split the given array into smaller arrays using JavaScript. For example [1, 2, 3, 4]
should be split to [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [2] [2, 3] [2, 3, 4] [3] [3, 4] [4]
.
I am using this code:
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++)
let a = ;
for (let j = 0; j < arr.length; j++)
a.push(arr[j]);
if (a.length === i)
break;
console.log(a);
And I get the following result: [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] undefined
What am I missing/doing wrong?
javascript arrays substring
 |Â
show 2 more comments
up vote
15
down vote
favorite
My task is to split the given array into smaller arrays using JavaScript. For example [1, 2, 3, 4]
should be split to [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [2] [2, 3] [2, 3, 4] [3] [3, 4] [4]
.
I am using this code:
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++)
let a = ;
for (let j = 0; j < arr.length; j++)
a.push(arr[j]);
if (a.length === i)
break;
console.log(a);
And I get the following result: [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] undefined
What am I missing/doing wrong?
javascript arrays substring
I think you should set j=i in begin loop
â Alexandr Kudryashov
Sep 17 at 8:14
@AlexandrKudryashov I have tried. It is not correct
â TeodorKolev
Sep 17 at 8:15
set j=i and remove the if condition in the nested loop
â rock star
Sep 17 at 8:16
init i with 0 and remove the = symbol in the corresponding condition
â rock star
Sep 17 at 8:17
1
@rockstar nope, it is not correct
â TeodorKolev
Sep 17 at 8:17
 |Â
show 2 more comments
up vote
15
down vote
favorite
up vote
15
down vote
favorite
My task is to split the given array into smaller arrays using JavaScript. For example [1, 2, 3, 4]
should be split to [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [2] [2, 3] [2, 3, 4] [3] [3, 4] [4]
.
I am using this code:
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++)
let a = ;
for (let j = 0; j < arr.length; j++)
a.push(arr[j]);
if (a.length === i)
break;
console.log(a);
And I get the following result: [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] undefined
What am I missing/doing wrong?
javascript arrays substring
My task is to split the given array into smaller arrays using JavaScript. For example [1, 2, 3, 4]
should be split to [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [2] [2, 3] [2, 3, 4] [3] [3, 4] [4]
.
I am using this code:
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++)
let a = ;
for (let j = 0; j < arr.length; j++)
a.push(arr[j]);
if (a.length === i)
break;
console.log(a);
And I get the following result: [1] [1, 2] [1, 2, 3] [1, 2, 3, 4] undefined
What am I missing/doing wrong?
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++)
let a = ;
for (let j = 0; j < arr.length; j++)
a.push(arr[j]);
if (a.length === i)
break;
console.log(a);
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++)
let a = ;
for (let j = 0; j < arr.length; j++)
a.push(arr[j]);
if (a.length === i)
break;
console.log(a);
javascript arrays substring
javascript arrays substring
edited Sep 17 at 10:06
Bergi
349k54509833
349k54509833
asked Sep 17 at 8:10
TeodorKolev
1,02052045
1,02052045
I think you should set j=i in begin loop
â Alexandr Kudryashov
Sep 17 at 8:14
@AlexandrKudryashov I have tried. It is not correct
â TeodorKolev
Sep 17 at 8:15
set j=i and remove the if condition in the nested loop
â rock star
Sep 17 at 8:16
init i with 0 and remove the = symbol in the corresponding condition
â rock star
Sep 17 at 8:17
1
@rockstar nope, it is not correct
â TeodorKolev
Sep 17 at 8:17
 |Â
show 2 more comments
I think you should set j=i in begin loop
â Alexandr Kudryashov
Sep 17 at 8:14
@AlexandrKudryashov I have tried. It is not correct
â TeodorKolev
Sep 17 at 8:15
set j=i and remove the if condition in the nested loop
â rock star
Sep 17 at 8:16
init i with 0 and remove the = symbol in the corresponding condition
â rock star
Sep 17 at 8:17
1
@rockstar nope, it is not correct
â TeodorKolev
Sep 17 at 8:17
I think you should set j=i in begin loop
â Alexandr Kudryashov
Sep 17 at 8:14
I think you should set j=i in begin loop
â Alexandr Kudryashov
Sep 17 at 8:14
@AlexandrKudryashov I have tried. It is not correct
â TeodorKolev
Sep 17 at 8:15
@AlexandrKudryashov I have tried. It is not correct
â TeodorKolev
Sep 17 at 8:15
set j=i and remove the if condition in the nested loop
â rock star
Sep 17 at 8:16
set j=i and remove the if condition in the nested loop
â rock star
Sep 17 at 8:16
init i with 0 and remove the = symbol in the corresponding condition
â rock star
Sep 17 at 8:17
init i with 0 and remove the = symbol in the corresponding condition
â rock star
Sep 17 at 8:17
1
1
@rockstar nope, it is not correct
â TeodorKolev
Sep 17 at 8:17
@rockstar nope, it is not correct
â TeodorKolev
Sep 17 at 8:17
 |Â
show 2 more comments
6 Answers
6
active
oldest
votes
up vote
11
down vote
accepted
You have two issues in your code:
- You need to have loop to initialize with the value of
i
for the inner loop so that it consider the next index for new iteration ofi
- You need to remove that
break
on the length which you have in inner loop.
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
It might be worth noting that this code is logging the same arraya
to the console on successive iterations of the inner loop, and modifying it in between. Admittedly, this is what the OP's code does, too, but it's likely to surprise anyone who tries to naively modify this code e.g. to collect all the subsequences in a single array and return it. Nina Scholz's answer provides a more easily generalizable solution using.slice()
.
â Ilmari Karonen
Sep 17 at 15:31
1
Outer loop should use<
instead of<=
. It currently does an extra iteration only to be broken on the next line where you checkj < arr.length
.
â Salman A
Sep 20 at 0:45
add a comment |Â
up vote
16
down vote
For the inner array, you could just start with the index of the outer array.
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = ;
for (i = 0; i < l; i++)
for (j = i; j < l; j++)
result.push(array.slice(i, j + 1));
console.log(result.map(a => a.join(' ')));
.as-console-wrapper max-height: 100% !important; top: 0;
2
I was expecting answer from you :)
â Ankit Agarwal
Sep 17 at 8:18
This do one big array. Target is to create small arrays
â TeodorKolev
Sep 17 at 8:19
what do you want with small arrays? instead of pushing, you could display the sub array.
â Nina Scholz
Sep 17 at 8:22
Like! +1, for SO console style's hack :D
â l2aelba
Sep 17 at 13:24
add a comment |Â
up vote
2
down vote
Try this
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
4
Why do you need thetmp
variable?
â Jenny O'Reilly
Sep 17 at 8:22
you can do it without tmp variable=)
â Alexandr Kudryashov
Sep 17 at 8:23
add a comment |Â
up vote
0
down vote
If you don't want to mutate your array.
let arr = [1, 2, 3, 4];
let res = ;
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a = [...a, arr[j]];
res = [...res, a];
console.log(res);
add a comment |Â
up vote
0
down vote
i have prepare stackblitz for this case.
let source = [1,2,3,4];
const output = ;
const arrayMultiplier = (source) =>
const eachValueArray = ;
source.forEach((item, index) =>
// Will push new array who will be sliced source array.
eachValueArray.push(source.slice(0, source.length - index));
);
//We reverse array to have right order.
return eachValueArray.reverse();
;
for(let i = 0; i <= source.length; i++)
output.push(...arrayMultiplier(source));
source.shift(); // Will recraft source array by removing first index.
//Don't forget last item.
output.push(source);
console.log(output);
Is not the most shorten solution but do the job
== update after code review ==
// [...]
const arrayMultiplier = (source) =>
// Will push new array who will be sliced source array.
// We reverse array to have right order.
return source.map((item, index) => source.slice(0, source.length - index)).reverse();
;
// [...]
AvoidforEach
+push
, usemap
instead
â Bergi
Sep 17 at 10:02
yes si true @Bergi
â Yanis-git
Sep 17 at 10:50
add a comment |Â
up vote
0
down vote
Use two iteration
- get slice array based on loop index.
- use sliced array and combine array element.
var arr = [1, 2, 3, 4];
let newArra =;
arr.map((x,i)=>
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
)
newArra.forEach(x=> console.log(x))
1
@EricDuminil Thanks, Made the required changes.
â Anoop
Sep 17 at 12:06
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
You have two issues in your code:
- You need to have loop to initialize with the value of
i
for the inner loop so that it consider the next index for new iteration ofi
- You need to remove that
break
on the length which you have in inner loop.
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
It might be worth noting that this code is logging the same arraya
to the console on successive iterations of the inner loop, and modifying it in between. Admittedly, this is what the OP's code does, too, but it's likely to surprise anyone who tries to naively modify this code e.g. to collect all the subsequences in a single array and return it. Nina Scholz's answer provides a more easily generalizable solution using.slice()
.
â Ilmari Karonen
Sep 17 at 15:31
1
Outer loop should use<
instead of<=
. It currently does an extra iteration only to be broken on the next line where you checkj < arr.length
.
â Salman A
Sep 20 at 0:45
add a comment |Â
up vote
11
down vote
accepted
You have two issues in your code:
- You need to have loop to initialize with the value of
i
for the inner loop so that it consider the next index for new iteration ofi
- You need to remove that
break
on the length which you have in inner loop.
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
It might be worth noting that this code is logging the same arraya
to the console on successive iterations of the inner loop, and modifying it in between. Admittedly, this is what the OP's code does, too, but it's likely to surprise anyone who tries to naively modify this code e.g. to collect all the subsequences in a single array and return it. Nina Scholz's answer provides a more easily generalizable solution using.slice()
.
â Ilmari Karonen
Sep 17 at 15:31
1
Outer loop should use<
instead of<=
. It currently does an extra iteration only to be broken on the next line where you checkj < arr.length
.
â Salman A
Sep 20 at 0:45
add a comment |Â
up vote
11
down vote
accepted
up vote
11
down vote
accepted
You have two issues in your code:
- You need to have loop to initialize with the value of
i
for the inner loop so that it consider the next index for new iteration ofi
- You need to remove that
break
on the length which you have in inner loop.
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
You have two issues in your code:
- You need to have loop to initialize with the value of
i
for the inner loop so that it consider the next index for new iteration ofi
- You need to remove that
break
on the length which you have in inner loop.
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
answered Sep 17 at 8:22
Ankit Agarwal
21.8k41841
21.8k41841
It might be worth noting that this code is logging the same arraya
to the console on successive iterations of the inner loop, and modifying it in between. Admittedly, this is what the OP's code does, too, but it's likely to surprise anyone who tries to naively modify this code e.g. to collect all the subsequences in a single array and return it. Nina Scholz's answer provides a more easily generalizable solution using.slice()
.
â Ilmari Karonen
Sep 17 at 15:31
1
Outer loop should use<
instead of<=
. It currently does an extra iteration only to be broken on the next line where you checkj < arr.length
.
â Salman A
Sep 20 at 0:45
add a comment |Â
It might be worth noting that this code is logging the same arraya
to the console on successive iterations of the inner loop, and modifying it in between. Admittedly, this is what the OP's code does, too, but it's likely to surprise anyone who tries to naively modify this code e.g. to collect all the subsequences in a single array and return it. Nina Scholz's answer provides a more easily generalizable solution using.slice()
.
â Ilmari Karonen
Sep 17 at 15:31
1
Outer loop should use<
instead of<=
. It currently does an extra iteration only to be broken on the next line where you checkj < arr.length
.
â Salman A
Sep 20 at 0:45
It might be worth noting that this code is logging the same array
a
to the console on successive iterations of the inner loop, and modifying it in between. Admittedly, this is what the OP's code does, too, but it's likely to surprise anyone who tries to naively modify this code e.g. to collect all the subsequences in a single array and return it. Nina Scholz's answer provides a more easily generalizable solution using .slice()
.â Ilmari Karonen
Sep 17 at 15:31
It might be worth noting that this code is logging the same array
a
to the console on successive iterations of the inner loop, and modifying it in between. Admittedly, this is what the OP's code does, too, but it's likely to surprise anyone who tries to naively modify this code e.g. to collect all the subsequences in a single array and return it. Nina Scholz's answer provides a more easily generalizable solution using .slice()
.â Ilmari Karonen
Sep 17 at 15:31
1
1
Outer loop should use
<
instead of <=
. It currently does an extra iteration only to be broken on the next line where you check j < arr.length
.â Salman A
Sep 20 at 0:45
Outer loop should use
<
instead of <=
. It currently does an extra iteration only to be broken on the next line where you check j < arr.length
.â Salman A
Sep 20 at 0:45
add a comment |Â
up vote
16
down vote
For the inner array, you could just start with the index of the outer array.
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = ;
for (i = 0; i < l; i++)
for (j = i; j < l; j++)
result.push(array.slice(i, j + 1));
console.log(result.map(a => a.join(' ')));
.as-console-wrapper max-height: 100% !important; top: 0;
2
I was expecting answer from you :)
â Ankit Agarwal
Sep 17 at 8:18
This do one big array. Target is to create small arrays
â TeodorKolev
Sep 17 at 8:19
what do you want with small arrays? instead of pushing, you could display the sub array.
â Nina Scholz
Sep 17 at 8:22
Like! +1, for SO console style's hack :D
â l2aelba
Sep 17 at 13:24
add a comment |Â
up vote
16
down vote
For the inner array, you could just start with the index of the outer array.
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = ;
for (i = 0; i < l; i++)
for (j = i; j < l; j++)
result.push(array.slice(i, j + 1));
console.log(result.map(a => a.join(' ')));
.as-console-wrapper max-height: 100% !important; top: 0;
2
I was expecting answer from you :)
â Ankit Agarwal
Sep 17 at 8:18
This do one big array. Target is to create small arrays
â TeodorKolev
Sep 17 at 8:19
what do you want with small arrays? instead of pushing, you could display the sub array.
â Nina Scholz
Sep 17 at 8:22
Like! +1, for SO console style's hack :D
â l2aelba
Sep 17 at 13:24
add a comment |Â
up vote
16
down vote
up vote
16
down vote
For the inner array, you could just start with the index of the outer array.
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = ;
for (i = 0; i < l; i++)
for (j = i; j < l; j++)
result.push(array.slice(i, j + 1));
console.log(result.map(a => a.join(' ')));
.as-console-wrapper max-height: 100% !important; top: 0;
For the inner array, you could just start with the index of the outer array.
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = ;
for (i = 0; i < l; i++)
for (j = i; j < l; j++)
result.push(array.slice(i, j + 1));
console.log(result.map(a => a.join(' ')));
.as-console-wrapper max-height: 100% !important; top: 0;
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = ;
for (i = 0; i < l; i++)
for (j = i; j < l; j++)
result.push(array.slice(i, j + 1));
console.log(result.map(a => a.join(' ')));
.as-console-wrapper max-height: 100% !important; top: 0;
var array = [1, 2, 3, 4],
i, j, l = array.length,
result = ;
for (i = 0; i < l; i++)
for (j = i; j < l; j++)
result.push(array.slice(i, j + 1));
console.log(result.map(a => a.join(' ')));
.as-console-wrapper max-height: 100% !important; top: 0;
edited Sep 17 at 20:58
answered Sep 17 at 8:17
Nina Scholz
161k1280141
161k1280141
2
I was expecting answer from you :)
â Ankit Agarwal
Sep 17 at 8:18
This do one big array. Target is to create small arrays
â TeodorKolev
Sep 17 at 8:19
what do you want with small arrays? instead of pushing, you could display the sub array.
â Nina Scholz
Sep 17 at 8:22
Like! +1, for SO console style's hack :D
â l2aelba
Sep 17 at 13:24
add a comment |Â
2
I was expecting answer from you :)
â Ankit Agarwal
Sep 17 at 8:18
This do one big array. Target is to create small arrays
â TeodorKolev
Sep 17 at 8:19
what do you want with small arrays? instead of pushing, you could display the sub array.
â Nina Scholz
Sep 17 at 8:22
Like! +1, for SO console style's hack :D
â l2aelba
Sep 17 at 13:24
2
2
I was expecting answer from you :)
â Ankit Agarwal
Sep 17 at 8:18
I was expecting answer from you :)
â Ankit Agarwal
Sep 17 at 8:18
This do one big array. Target is to create small arrays
â TeodorKolev
Sep 17 at 8:19
This do one big array. Target is to create small arrays
â TeodorKolev
Sep 17 at 8:19
what do you want with small arrays? instead of pushing, you could display the sub array.
â Nina Scholz
Sep 17 at 8:22
what do you want with small arrays? instead of pushing, you could display the sub array.
â Nina Scholz
Sep 17 at 8:22
Like! +1, for SO console style's hack :D
â l2aelba
Sep 17 at 13:24
Like! +1, for SO console style's hack :D
â l2aelba
Sep 17 at 13:24
add a comment |Â
up vote
2
down vote
Try this
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
4
Why do you need thetmp
variable?
â Jenny O'Reilly
Sep 17 at 8:22
you can do it without tmp variable=)
â Alexandr Kudryashov
Sep 17 at 8:23
add a comment |Â
up vote
2
down vote
Try this
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
4
Why do you need thetmp
variable?
â Jenny O'Reilly
Sep 17 at 8:22
you can do it without tmp variable=)
â Alexandr Kudryashov
Sep 17 at 8:23
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Try this
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
Try this
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
let arr = [1, 2, 3, 4];
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a.push(arr[j]);
console.log(a);
edited Sep 18 at 8:30
answered Sep 17 at 8:19
Alexandr Kudryashov
567213
567213
4
Why do you need thetmp
variable?
â Jenny O'Reilly
Sep 17 at 8:22
you can do it without tmp variable=)
â Alexandr Kudryashov
Sep 17 at 8:23
add a comment |Â
4
Why do you need thetmp
variable?
â Jenny O'Reilly
Sep 17 at 8:22
you can do it without tmp variable=)
â Alexandr Kudryashov
Sep 17 at 8:23
4
4
Why do you need the
tmp
variable?â Jenny O'Reilly
Sep 17 at 8:22
Why do you need the
tmp
variable?â Jenny O'Reilly
Sep 17 at 8:22
you can do it without tmp variable=)
â Alexandr Kudryashov
Sep 17 at 8:23
you can do it without tmp variable=)
â Alexandr Kudryashov
Sep 17 at 8:23
add a comment |Â
up vote
0
down vote
If you don't want to mutate your array.
let arr = [1, 2, 3, 4];
let res = ;
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a = [...a, arr[j]];
res = [...res, a];
console.log(res);
add a comment |Â
up vote
0
down vote
If you don't want to mutate your array.
let arr = [1, 2, 3, 4];
let res = ;
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a = [...a, arr[j]];
res = [...res, a];
console.log(res);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you don't want to mutate your array.
let arr = [1, 2, 3, 4];
let res = ;
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a = [...a, arr[j]];
res = [...res, a];
console.log(res);
If you don't want to mutate your array.
let arr = [1, 2, 3, 4];
let res = ;
for (let i = 0; i <= arr.length; i++)
let a = ;
for (let j = i; j < arr.length; j++)
a = [...a, arr[j]];
res = [...res, a];
console.log(res);
edited Sep 17 at 9:48
Yanis-git
1,4471318
1,4471318
answered Sep 17 at 9:32
Varun Arya
11
11
add a comment |Â
add a comment |Â
up vote
0
down vote
i have prepare stackblitz for this case.
let source = [1,2,3,4];
const output = ;
const arrayMultiplier = (source) =>
const eachValueArray = ;
source.forEach((item, index) =>
// Will push new array who will be sliced source array.
eachValueArray.push(source.slice(0, source.length - index));
);
//We reverse array to have right order.
return eachValueArray.reverse();
;
for(let i = 0; i <= source.length; i++)
output.push(...arrayMultiplier(source));
source.shift(); // Will recraft source array by removing first index.
//Don't forget last item.
output.push(source);
console.log(output);
Is not the most shorten solution but do the job
== update after code review ==
// [...]
const arrayMultiplier = (source) =>
// Will push new array who will be sliced source array.
// We reverse array to have right order.
return source.map((item, index) => source.slice(0, source.length - index)).reverse();
;
// [...]
AvoidforEach
+push
, usemap
instead
â Bergi
Sep 17 at 10:02
yes si true @Bergi
â Yanis-git
Sep 17 at 10:50
add a comment |Â
up vote
0
down vote
i have prepare stackblitz for this case.
let source = [1,2,3,4];
const output = ;
const arrayMultiplier = (source) =>
const eachValueArray = ;
source.forEach((item, index) =>
// Will push new array who will be sliced source array.
eachValueArray.push(source.slice(0, source.length - index));
);
//We reverse array to have right order.
return eachValueArray.reverse();
;
for(let i = 0; i <= source.length; i++)
output.push(...arrayMultiplier(source));
source.shift(); // Will recraft source array by removing first index.
//Don't forget last item.
output.push(source);
console.log(output);
Is not the most shorten solution but do the job
== update after code review ==
// [...]
const arrayMultiplier = (source) =>
// Will push new array who will be sliced source array.
// We reverse array to have right order.
return source.map((item, index) => source.slice(0, source.length - index)).reverse();
;
// [...]
AvoidforEach
+push
, usemap
instead
â Bergi
Sep 17 at 10:02
yes si true @Bergi
â Yanis-git
Sep 17 at 10:50
add a comment |Â
up vote
0
down vote
up vote
0
down vote
i have prepare stackblitz for this case.
let source = [1,2,3,4];
const output = ;
const arrayMultiplier = (source) =>
const eachValueArray = ;
source.forEach((item, index) =>
// Will push new array who will be sliced source array.
eachValueArray.push(source.slice(0, source.length - index));
);
//We reverse array to have right order.
return eachValueArray.reverse();
;
for(let i = 0; i <= source.length; i++)
output.push(...arrayMultiplier(source));
source.shift(); // Will recraft source array by removing first index.
//Don't forget last item.
output.push(source);
console.log(output);
Is not the most shorten solution but do the job
== update after code review ==
// [...]
const arrayMultiplier = (source) =>
// Will push new array who will be sliced source array.
// We reverse array to have right order.
return source.map((item, index) => source.slice(0, source.length - index)).reverse();
;
// [...]
i have prepare stackblitz for this case.
let source = [1,2,3,4];
const output = ;
const arrayMultiplier = (source) =>
const eachValueArray = ;
source.forEach((item, index) =>
// Will push new array who will be sliced source array.
eachValueArray.push(source.slice(0, source.length - index));
);
//We reverse array to have right order.
return eachValueArray.reverse();
;
for(let i = 0; i <= source.length; i++)
output.push(...arrayMultiplier(source));
source.shift(); // Will recraft source array by removing first index.
//Don't forget last item.
output.push(source);
console.log(output);
Is not the most shorten solution but do the job
== update after code review ==
// [...]
const arrayMultiplier = (source) =>
// Will push new array who will be sliced source array.
// We reverse array to have right order.
return source.map((item, index) => source.slice(0, source.length - index)).reverse();
;
// [...]
edited Sep 17 at 10:56
answered Sep 17 at 8:35
Yanis-git
1,4471318
1,4471318
AvoidforEach
+push
, usemap
instead
â Bergi
Sep 17 at 10:02
yes si true @Bergi
â Yanis-git
Sep 17 at 10:50
add a comment |Â
AvoidforEach
+push
, usemap
instead
â Bergi
Sep 17 at 10:02
yes si true @Bergi
â Yanis-git
Sep 17 at 10:50
Avoid
forEach
+ push
, use map
insteadâ Bergi
Sep 17 at 10:02
Avoid
forEach
+ push
, use map
insteadâ Bergi
Sep 17 at 10:02
yes si true @Bergi
â Yanis-git
Sep 17 at 10:50
yes si true @Bergi
â Yanis-git
Sep 17 at 10:50
add a comment |Â
up vote
0
down vote
Use two iteration
- get slice array based on loop index.
- use sliced array and combine array element.
var arr = [1, 2, 3, 4];
let newArra =;
arr.map((x,i)=>
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
)
newArra.forEach(x=> console.log(x))
1
@EricDuminil Thanks, Made the required changes.
â Anoop
Sep 17 at 12:06
add a comment |Â
up vote
0
down vote
Use two iteration
- get slice array based on loop index.
- use sliced array and combine array element.
var arr = [1, 2, 3, 4];
let newArra =;
arr.map((x,i)=>
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
)
newArra.forEach(x=> console.log(x))
1
@EricDuminil Thanks, Made the required changes.
â Anoop
Sep 17 at 12:06
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Use two iteration
- get slice array based on loop index.
- use sliced array and combine array element.
var arr = [1, 2, 3, 4];
let newArra =;
arr.map((x,i)=>
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
)
newArra.forEach(x=> console.log(x))
Use two iteration
- get slice array based on loop index.
- use sliced array and combine array element.
var arr = [1, 2, 3, 4];
let newArra =;
arr.map((x,i)=>
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
)
newArra.forEach(x=> console.log(x))
var arr = [1, 2, 3, 4];
let newArra =;
arr.map((x,i)=>
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
)
newArra.forEach(x=> console.log(x))
var arr = [1, 2, 3, 4];
let newArra =;
arr.map((x,i)=>
let remainArr = arr.slice(i);
return remainArr.forEach((y, r) => newArra.push(remainArr.slice(0, r+1)))
)
newArra.forEach(x=> console.log(x))
edited Sep 17 at 12:05
answered Sep 17 at 8:29
Anoop
18.8k94568
18.8k94568
1
@EricDuminil Thanks, Made the required changes.
â Anoop
Sep 17 at 12:06
add a comment |Â
1
@EricDuminil Thanks, Made the required changes.
â Anoop
Sep 17 at 12:06
1
1
@EricDuminil Thanks, Made the required changes.
â Anoop
Sep 17 at 12:06
@EricDuminil Thanks, Made the required changes.
â Anoop
Sep 17 at 12:06
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52363370%2fhow-to-get-all-substrings-contiguous-subsequences-of-my-javascript-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I think you should set j=i in begin loop
â Alexandr Kudryashov
Sep 17 at 8:14
@AlexandrKudryashov I have tried. It is not correct
â TeodorKolev
Sep 17 at 8:15
set j=i and remove the if condition in the nested loop
â rock star
Sep 17 at 8:16
init i with 0 and remove the = symbol in the corresponding condition
â rock star
Sep 17 at 8:17
1
@rockstar nope, it is not correct
â TeodorKolev
Sep 17 at 8:17