Javascript array splitting with beginning words like consecutive numbers

Viewed 148

can you help me? I have and array named baseArray. As you see some values are same, I tried to remove duplicate values, but not fully worked.

Then I tried to split arrays like

[[1,2,3,4,5,6,7], [1,2,4,5,6,7], [1,2,3,4,5,6,7,8,9,10..]..]

I want to split each number series begin with number 1

const editedArray = [];

const baseArray = [1, 2, 3, 4, 5, 6, 7, 7, 7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1,
    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7,
    7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
    13, 13, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12,
    12, 13, 13, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4,
    5, 6, 7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
    10, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4, 5, 6, 7,
    8, 9, 9, 9, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1, 2, 3, 4, 5, 6,
    7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
    8, 8, 9, 9, 10, 10, 11, 11, 1, 2, 3, 4, 5, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
]
baseArray.filter(function (item, pos) {
    return baseArray.indexOf(item) == pos;
})

baseArray.forEach(item => item === 1 ?
    editedArray.push([1]) :
    editedArray[editedArray.length - 1].push(item)
)

console.log(editedArray)

This gives me as a result like enter image description here

Updated, It works fine now

const editedArray = [];

const baseArray = [1, 2, 3, 4, 5, 6, 7, 7, 7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1,
    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7,
    7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
    13, 13, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12,
    12, 13, 13, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4,
    5, 6, 7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
    10, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4, 5, 6, 7,
    8, 9, 9, 9, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1, 2, 3, 4, 5, 6,
    7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
    8, 8, 9, 9, 10, 10, 11, 11, 1, 2, 3, 4, 5, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
]
var result = baseArray.filter((a, i, aa) => a !== aa[i + 1]);

result.forEach(item => item === 1 ?
    editedArray.push([1]) :
    editedArray[editedArray.length - 1].push(item)
)
console.log(editedArray)
5 Answers

Using Array#reduce

const baseArray = [1,2,3,4,5,6,7,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,2,3,4,5,6,7,8,9,10,11,12,12,12,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,1,2,3,4,5,6,7,8,9,10,11,12,13,13,13,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,1,2,3,4,5,6,6,6,1,1,2,2,3,3,4,4,5,5,6,6,1,2,3,4,5,6,7,8,9,10,10,10,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,1,2,3,4,5,6,6,6,1,1,2,2,3,3,4,4,5,5,6,6,1,2,3,4,5,6,7,8,9,9,9,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,1,2,3,4,5,6,7,8,9,10,10,10,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,1,2,3,4,5,6,7,8,9,10,11,11,11,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,1,2,3,4,5,5,5,1,1,2,2,3,3,4,4,5,5];

const editedArray = baseArray.reduce((acc, num, i, arr) => {
  if (arr[i - 1] !== num) {
    if (num === 1) acc.push([num]);
    else acc[acc.length - 1].push(num);
  }
  
  return acc;
}, []);

console.log(editedArray);

Example below:

const baseArray = [ 1, 2, 3, 4, 5, 6, 7, 7, 7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 13, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 1, 2, 3, 4, 5, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, ];

const output = baseArray
  .join(",")
  .split(",1,")
  .map(el => Array.from(new Set(("1," + el).split(","))));

console.log(output);

Your filter is wrong. You can use

filter((item, pos) => !pos || baseArray[pos - 1] !== item)

to remove consecutive duplicates:

const baseArray = [1, 2, 3, 4, 5, 6, 7, 7, 7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1,
    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7,
    7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
    13, 13, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12,
    12, 13, 13, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4,
    5, 6, 7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
    10, 1, 2, 3, 4, 5, 6, 6, 6, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 2, 3, 4, 5, 6, 7,
    8, 9, 9, 9, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1, 2, 3, 4, 5, 6,
    7, 8, 9, 10, 10, 10, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
    8, 8, 9, 9, 10, 10, 11, 11, 1, 2, 3, 4, 5, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
]
const editedArray = [];

baseArray.filter((item, pos) => !pos || baseArray[pos - 1] !== item)
         .forEach(item => item === 1 ?
    editedArray.push([1]) :
    editedArray[editedArray.length - 1].push(item)
)

console.log(editedArray);

I guess this solution will work. This is in a single loop.

let editedArr = [];
let subArr = [];
let hash = {};
baseArray.forEach((item, index) => {
  if(index) {
    /* If you want to remove 1 as a array */
    /* Change the below condition to */
    /* if((item === 1 && baseArray[index - 1] !== 1) || index === baseArray.length - 1) */
    if(item === 1 || index === baseArray.length - 1) {
      editedArr.push(subArr);
      subArr = [item];
        hash = {
          [item]: true,
        };
      
    } else if (!(item in hash)) {
         subArr.push(item);
         hash[item] = true;
    }
  } else {
    subArr.push(item);
    hash[item] = true;
  }
});

console.log({editedArr});

You must remove filter statement and after first forEach you can use another forEach for editedArray and remove duplicate element from each array in editedArray:

const finalArray = [];
editedArray.forEach(item => {
  finalArray.push([...new Set(item)]);
})
Related