How to take every 3rd element of an array [duplicate] - javascript

This question already has answers here:
Javascript: take every nth Element of Array
(6 answers)
Closed 6 years ago.
I have an array, and want to return only every third element as a new array (starting at 0).
For example:
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let newArr = [1, 4, 7];
This is the way I am currently doing this:
let newArr = [];
for(let x = 0; x < arr.length; x += 3) {
newArr.push(arr[x]);
}
return newArr;
Is there a way to do this with arr.map? Is there just an easier way to do this?

You can alternatively do it with a filter,
let newArr = arr.filter((_,i) => i % 3 == 0);
But remember, using basic for loop is bit more efficient than others in some contexts.

Related

change index in one array in accordance with another one js [duplicate]

This question already has answers here:
Sort two arrays the same way
(12 answers)
Closed 18 days ago.
I need to change indexes of elements in arr a due to their indexes in arr b.
const a = [4,3,2,1,5];
const b = [1,2,3,4,5];
console.log(a) [1,2,3,4,5]
If you mean ordering array a according to array b, then you can do like this:
a.forEach((element,i) => {
// first get the index of a[i] from array b
const index = b.indexOf(a[i])
// then swap them
const temp = a[index];
a[index] = a[i];
a[i] = temp;
})
You could sort by using the other array as index. If this daoes not work with real data, please andd a small amount of data to highlight the problem.
const
a = [4, 3, 2, 1, 5],
b = [1, 2, 3, 4, 5];
a.sort((l, r) => b[l - 1] - b[r - 1]);
console.log(...a);

Hello everyone, I am working through a program and stuck on a challenge and cannot figure out why this will not work [duplicate]

This question already has answers here:
How are for loops executed in javascript?
(6 answers)
Closed last year.
Task: Use a for loop to iterate through the array and increase each number by two.
const increaseByTwo = [1, 2, 3, 4, 5];
What is wrong with my code here?
for (let i = 0; i < increaseByTwo.length; i+=2){
}
is this what you need?
const increaseByTwo = [1, 2, 3, 4, 5];
var newArr = [];
increaseByTwo.forEach(e=>newArr.push(e+2))
console.log(newArr)

Check whether every item in an array is a power of 2 [duplicate]

This question already has answers here:
What is the best way to determine if a given number is a power of two?
(8 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I can't seem to put this problem together,
Return an array containing all indices that are powers of 2
What I have below so far, I know modulo is probably the wrong operate, but I was trying to make it work. I'm not sure what else to try here.
function secondPower(arr) {
let newarr = [];
for(let i = 0; i < arr.length; i++){
if(arr[i] / (2 ** i) === 1){
newarr.push(i);
}
}
return newarr;
}
[1, 2, 4, 5, 7, 16]
You can use Math.log2 to check whether the number is a power of 2, and Array.every to check whether every item in the array matches a condition:
function isPowerOf2(x) {
return Math.log2(x) % 1 === 0;
}
const arr = [1, 2, 4, 5, 7, 16]
const arr2 = [2, 4, 8]
function isArrAllPowerOf2(arr){
return arr.every(isPowerOf2)
}
console.log(isArrAllPowerOf2(arr))
console.log(isArrAllPowerOf2(arr2))

convert number to array in javascript | typescript for angular7 [duplicate]

This question already has answers here:
How to initialize an array's length in JavaScript?
(20 answers)
How to create an array containing 1...N
(77 answers)
Closed 4 years ago.
i have a number 10 how to convert to this to number array in java script.
answer should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
OR How to convert number(10) to number of items in array in javascript
A simple for loop will easily accomplish this task.
var number = 10;
var arr = [];
for(var i = 0; i < number; i++) arr.push(i+1);
console.log(arr)
You can also use:
Array.from(new Array(number), (x,i) => i+1)
var number = 10;
var arr = Array.from(new Array(number), (x,i) => i+1)
console.log(arr)
Lets say you have a variable let a which implies your last number in array and at the same time number of elements in array.
let a = 10;
Now initialize an array which you will populate with numbers
let arr = [];
Now do a for loop and push the numbers in the array.
for (let i = 0; i < a; i++)
arr.push(i+1);
This will populate array arr with numbers from 1 to 10.

How do I subtract 1 from each number in an array? [duplicate]

This question already has answers here:
Looping Through An Array and Dividing Each Value By 100
(5 answers)
Closed 7 years ago.
If I have
var numberarr = [1, 2, 3, 4, 5];
How would i make it into
var numberarr2 = [0, 1, 2, 3, 4];
by decrementing 1 from each element?
You can use .map( )
var numberarr2 = numberarr.map( function(value) {
return value - 1;
} );
Try this:
// Create an array to hold our new values
var numberArr2 = [];
// Iterate through each element in the original array
for(var i = 0; i < numberArr1.length; i++) {
// Decrement the value of the original array and push it to the new one
numberArr2.push(numberArr1[i] - 1);
}

Categories

Resources