I have an array like below
const num = [1,2,3,4,5,6,7];
and boundary condition values,
var first = 6
var second = 3
Expected Output now based on this is like below,
[6,7,1,2,3]
Explain:
The result should combine 2 arrays:
>= first
in the left[6, 7]
<= second
in the right[1,2,3]
I tried trying something like below but array positions are not as I mentioned above.
const result = num.filter((n,p) => {
if(p >= num.indexOf(first)) {return n}
else if(p <= num.indexOf(second)) {return n}
});
console.log(result) // [1,2,3,6,7]
an alternative way is below but that is not efficient as I need to loop twice.
const num = [1,2,3,4,5,6,7];
var first = 6
var second = 3
var arr = []
num.forEach((n,p) => {
if(p >= num.indexOf(first)) {arr.push(n)}
});
num.forEach((n,p) => {
if(p <= num.indexOf(second)) {arr.push(n)}
});
console.log(arr) //[6,7,1,2,3]
Any better way to achieve this?