0

I have an array consisting of sub arrays:

myArray  = [[info1, info2, info3, value1], [info1, info2, info3, value1], 
[info1, info2, info3, value1],.............]

I want to split the array into several arrays based on "value1". If value1 is above 1000 in more then 5 consecutive elements i want to create a new array:

mySubArray1 = [[info1, info2, info3, 1011], [info1, info2, info3, 
1012], [info1, info2, info3, 1011],.............]

and so if i discover a second series of elements where value1 is above 1000 in more then 5 consecutive elements i want to create another array:

mySubArray2 = [[info1, info2, info3, 3030], [info1, info2, info3, 
4000], [info1, info2, info3, 1700],.............]

What i have done is the following:

var j;
for (j = 0; j < array.length; j++) {
if (array[j][3] < 1000 ) {

    console.log("below 1000");
}
else {
    console.log("above 1000");
}

}

This detects where value is above 1000, but now i need to sort it into new arrays, and i don't know how to start that process.

1
  • What are info1, info2, info3, etc? Are those all standalone variables too? (if you meant to use strings, strings need delimiters) You should also post the code you've tried that isn't working Commented Jan 23, 2019 at 7:08

1 Answer 1

2

You should be able to do an Array.reduce to get the desired result:

I'm generating an array that should be like your input, the value of 'value1' will be between 995 and 1010.

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

let myArray  = [...Array(100).keys()].map(n => ['info1', 'info2', 'info3', getRandomInt(995, 1010)]);

console.log("Input: ", JSON.stringify(myArray));

let subArray = [];
// Result will contain a list of our new subarrays.
let result = myArray.reduce((acc, val, index) => {

   if (val[3] >= 1000) { 
      subArray.push(val);
   } 

   if ((val[3] < 1000) || (index === myArray.length - 1) /* Handle the case when we're at the end of myArray */ ) {
       if (subArray.length > 5) { 
           acc.push(subArray);
       } 
       subArray = [];
   }
 
   return acc;
}, [])

console.log("Result:");
result.forEach( (subArray, n) => console.log(`Subarray ${n+1}: `, subArray));

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

3 Comments

Thanks. I partly understand this and it partly works. But now, if the lenght of the consequetiuve series is longer then 5, it will break the array regardless on subArray.length = 5 ? So if it is a series of 8 it will actually create a subarray with 5... (My goal in this case is to get all 8 in the same subArray) I think maybe something like this could solve it: if (subArray.length > 5 && val[3]<1000) { acc.push(subArray); subArray = []; }
I've updated the answer with new logic, it should create subarrays of 5+ items.
Thanks again! Series longer then 5 is now appended to subarrays. It might still have one issue: If the last value is supposed to go into the subarray, the last subarray will be disregarded because of: (index === myArray.length - 1) ..............(I think).

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.