const arr = [1, 6, 43, 3, 56, 0, 9, 98]; function composeNewArray(arr, l) { const newArr = []; for (let i=0;i<l;i++) { while(1) { let index = Math.floor(Math.random() * (arr.length - 1)); if (newArr.indexOf(arr[index])!== -1) { continue; } else { newArr.push(arr[index]); break; } } } return newArr; } const newArr = composeNewArray(arr, 4); console.log(newArr);