-1

I have an array of arrays like this, const array = [[1,5],[7,9],[10,14]]; I need to check to see if the arrays are in order.

Basically, for the provided array, I need to compare the first array's second value, (5), with the second array's first value, (7), which I would then use to invalidate a form.

If the arrays or in order, const array = [[1,5],[6,9],[10,14]]; it returns false, it passes validation, if they aren't, validation fails and it returns true.

So far, I've done something like this.

const arrayIdxZeroValues = [];
const arrayIdxOneValues = []; 
array.forEach((arr,idx) => {
  arrayIdxZeroValues.push(arr[0])
  arrayIdxOneValues.push(arr[1])
})

which separates the values fine. I then thought about using .shift() to remove the first value of arrayIdxZeroValues since it will never need to be compared, but I feel this is hacky and I'm not sure it's the right approach from here. Maybe .reduce() to compare the output arrays, but I haven't been able to get it to work right.

Any guidance would be much appreciated.

Thanks

Adam Bradbury
  • 91
  • 2
  • 11

2 Answers2

1

You could check the actual value agains the last value of the previous array plus one and omit the check for the first inner array.

const
    inOrder = array => array.every(([v], i, a) => !i || a[i - 1][1] + 1 === v);

console.log(inOrder([[1, 5], [7, 9], [10, 14]]));
console.log(inOrder([[1, 5], [6, 9], [10, 14]]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Really appreciate the help! If you find the time, I'd love an explanation of the implementation. – Adam Bradbury Dec 11 '19 at 23:12
  • I'm still getting a true value returned when there are no gaps, however. ```const array = [[1, 5], [6, 9], [10, 14]]``` – Adam Bradbury Dec 12 '19 at 17:48
  • it looks like `true`. do you want `false`? why? – Nina Scholz Dec 12 '19 at 17:50
  • because I need to determine if the subarrays are continuous. In your example array, there is a gap between 5 and 7, so it should return true, as it does. In my example there is no gap, 5 to 6, 9 to 10, and so one. This should return false, meaning there is no gap between the subarrays. – Adam Bradbury Dec 12 '19 at 18:37
  • do you need only to check the gaps? or if the value inside of the array are greater than the first value? – Nina Scholz Dec 12 '19 at 18:38
1

Create an array that starts from the 2nd sub-array using Array.slice(). Use Array.some() and check if the 2nd item of the current sub-array is equal or less the 2nd item of the sub-array at the current index i of the original array. In this way you compare the 2nd with the 1st, the 3rd with the 2nd, and so on.

false - means the validation passed true - means validation failed

const isFailed = arr => arr.slice(1)
  .some(([n], i) => n !== arr[i][1] + 1)


console.log(isFailed([[1,5],[6,9],[10,14]])) // false
console.log(isFailed([[1,5],[7,9],[10,14]])) // true
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • Thanks Ori, but I don't believe this works given the criteria. The first array, [1,5] would need to then start with a 6, [6,9] to pass validation. Given that array, the third array would need to start with 10, [10,14], and so on for an undetermined number of subarrays – Adam Bradbury Dec 12 '19 at 17:44
  • So the distance between last and 1st should be 1 always? – Ori Drori Dec 12 '19 at 18:06
  • yes. Basically, continuous. The sub array values represent ranges, with a lower and upper value. They can't overlap, for example, [[1,4], [4,7], .........] wouldn't qualify. The second subarray would need to be a 5, [5,7]. If it's [6,7] that wouldn't qualify either. And the number of subarrays has no limit. – Adam Bradbury Dec 12 '19 at 18:33
  • Updated the answer. What is that used for? and why do you want `false` when the criteria is met, and `true` when it fails? – Ori Drori Dec 12 '19 at 18:36
  • 1
    Thanks again! I use it in a form validation. The way the validation works, is if it return true, it throws a form error. For example, if the input field is required, and the value === '', the logic return true and the form renders the UI message. – Adam Bradbury Dec 12 '19 at 19:29