0

I am trying to get the array in the below format, However anything I try breaks one thing or the other

const arr = ['0', '1000', '2000', '3000', '4000'];
const desiredFormat = [];
const chunkSize = 2;
while (arr.length > 0) {
desiredFormat.push(arr.splice(0, chunkSize).join(","));
}
console.log("RESSSULTTT",desiredFormat)

Actual Value returned

['0,1000', '2000,3000', '4000']

Desired Format

['0,1000','2000,3000', '3000,4000']
Phil
  • 435
  • 2
  • 9
  • 28
  • 1
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+overlapping+array+chunks+of+length+2) of [Split array into overlapping chunks (moving subgroups)](/q/14985948/4642212). Then just put a `.join(",")` at the appropriate spot. – Sebastian Simon Sep 08 '21 at 15:05
  • Please post an answer it's not that simple :( – Phil Sep 08 '21 at 15:07
  • @Phil, that dup is more general than what you're aiming for, handling not just pairs but triplets and so on, but it's definitely a dup. – danh Sep 08 '21 at 15:10
  • 1
    @Phil Why is it not simple? There’s a `chunk` function in the accepted answer. Take it and call `chunk(arr, 2);`. Put `.join(",")` inside the `.push` call. – Sebastian Simon Sep 08 '21 at 15:10

4 Answers4

2

Something simple using the 2nd argument of the map callback you can get the index to query another element.

const data = ['0', '1000', '2000', '3000', '4000'];

const result = data.slice(1).map((el, index) => `${data[index]},${el}`);
console.log(result);

I'm using slice to remove the first element which would combine element at index negative one with zero.

Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
  • Awesome let me test it out if it works in all cases I'll accept – Phil Sep 08 '21 at 15:08
  • 1
    I edited the answer to slice the output (removing unnecessary element) before mapping instead of after – Krzysztof Krzeszewski Sep 08 '21 at 15:10
  • This is more concise, more declarative code than the accepted dup answer. Good answer. According to the (overly up-tight, IMO) rules, though, it really ought to be an answer on the other post. – danh Sep 08 '21 at 15:17
  • As requested @danh I have added my answer to the beforementioned dupe, as well as extended it to work with chunks of different sizes – Krzysztof Krzeszewski Sep 09 '21 at 08:32
1

Demonstrating the dup with a snippet:

const arr = ['0', '1000', '2000', '3000', '4000'];
function chunk(array, chunkSize) {
    var retArr = [];
    for (var i = 0; i < array.length - (chunkSize - 1); i++) {
        retArr.push(array.slice(i, i + chunkSize));
    }
    return retArr;
}

console.log(chunk(arr, 2).map(el => el.join(',')));
danh
  • 62,181
  • 10
  • 95
  • 136
0

Here you go:

const arr = ['0', '1000', '2000', '3000', '4000'];
const newArray = [];

for(let i = 0; i < arr.length - 1; i++) {
  newArray.push(arr[i] + "," + arr[i + 1]);
}

console.log(newArray);
0

Solution

let i=0; 
desiredFormat=[];
const arr = ['0', '1000', '2000', '3000', '4000'];
while (arr.length-1 > i) {
  desiredFormat.push(`${arr[i]}, ${arr[i+1]}`);
   i++
}
console.log("RESSSULTTT",desiredFormat)

> "RESSSULTTT" ["0, 1000", "1000, 2000", "2000, 3000", "3000, 4000"]
Prabhunath Yadav
  • 185
  • 1
  • 14