onlycode.org

javascript
jquery
html
angularjs
css
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
how to get all substring...

How to get all substrings (contiguous subsequences) of my JavaScript array, just only code?

substring | arrays | javascript

edit
    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);

27
Yanis-git, 17.09.2018 12:32



edit
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);
// [...]
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();
};
// [...]

11
Yanis-git, 17.09.2018 11:35

Add your just only code

your name:    



If you need full text click here

Some similar links How to get all substrings (contiguous subsequences) of my JavaScript array? ↓:
How to check if a string contains text from an array of substrings in JavaScript?

How to extend an existing JavaScript array with another array, without creating a new array?

Sum of array object property values in new array of objects in Javascript

How to convert simple array into two-dimensional array(matrix) in javascript (or jquery)



This project is created for those who just need only code, wihout maaaaaaaaaaany words.