Im getting an Array like this [[2,4],[7,32],[76,44],[34,22]...] up to 100, i want to get 5 Arrays out of all arrays, with the index of 1 in each of the array. am getting the index of 1 in each item in the arrays but how can i get the five different item, i want to select 3 random elements(plus the first and last one). can someone please help me out.
i want to the output to be something like this
4 44 30 77 66
Here's my code
const items = [[2,4],[7,32],[76,44],[34,22],[10,30],[34,67],[90,13],[20,14],[78,77],[9,77],[44,66]]
items.map(item => console.log(item[1]))
Take 3 random items and the first and the last item from the items array and return the index 1 from each item.
items = [[2,4],[7,32],[76,44],[34,22],[10,30],[34,67],[90,13],[20,14],[78,77],[9,77],[44,66]]
function action(arr) {
const f = arr[0];
const l = arr[arr.length-1]
const a = arr.slice(1, arr.length-1)
const t = [];
for(i=0; i<3;i++) {
t.push(a[Math.floor(Math.random()*a.length)][1]);
}
return [f[1], ...t, l[1]];
}
console.log(action(items))
slice() would the right approach.
const items = [[2,4],[7,32],[76,44],[34,22],[10,30],[34,67],[90,13],[20,14],[78,77],[9,77],[44,66]]
function action(arr, from, to) {
return arr.slice(from, to)
}
console.log(action(items, 1, 6));