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);
}
Related
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);
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.
This question already has answers here:
Rotate the elements in an array in JavaScript
(42 answers)
Closed 4 years ago.
I have an array,
var myArray = [ 1,2,3,4,5 ]
and variable count,
var count = 5
Pseudocode :
if count = 1, output myArray = [5,1,2,3,4]
if count = 2, then myArray = [ 4,5,1,2,3]
and so on ..
How can I achieve this without using loops ?
You could slice with a negative index from the end the array for the last part and the first part and concat a new array.
function move(array, i) {
return array.slice(-i).concat(array.slice(0, -i));
}
var array = [1, 2, 3, 4, 5];
console.log(move(array, 1)); // [5, 1, 2, 3, 4].
console.log(move(array, 2)); // [4, 5, 1, 2, 3]
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use pop for removing last item of the array and unshift for adding it at the beginning of the arrray then
const count = 2;
const myArray = [ 1,2,3,4,5 ];
for (let i = 0; i < count; i++) {
myArray.unshift(myArray.pop());
}
console.log(myArray);
This question already has answers here:
How to split a long array into smaller arrays, with JavaScript
(26 answers)
Split array into chunks
(73 answers)
Closed 4 years ago.
I am trying to split an array of objects into sets of 3 and there is a slight problem. I run the code and it splits the first group into 2 and the rest into 3. I want the last group to have the remaining elements. So for instance if there are 21 objects then the last group should have 2 but instead the first group is the one with 2. How can I make the last group be the one with the remaining objects?
var newData = [];
var setOfThree = [];
for (i = 0; i < data.length; i++) {
setOfThree.push(data[i]);
if (i % 3 == 1) {
newData.push(setOfThree);
setOfThree = [];
}
}
So the data ends up looking like this:
Here's a very clean and efficient solution (leverages the fact that Array.prototype.slice automatically truncates results):
let groupByN = (n, data) => {
let result = [];
for (let i = 0; i < data.length; i += n) result.push(data.slice(i, i + n));
return result;
};
console.log(JSON.stringify(groupByN(3, [ 1 ])));
console.log(JSON.stringify(groupByN(3, [ 1, 2 ])));
console.log(JSON.stringify(groupByN(3, [ 1, 2, 3 ])));
console.log(JSON.stringify(groupByN(3, [ 1, 2, 3, 4 ])));
console.log(JSON.stringify(groupByN(3, [ 1, 2, 3, 4, 5 ])));
console.log(JSON.stringify(groupByN(3, [ 1, 2, 3, 4, 5, 6 ])));
console.log(JSON.stringify(groupByN(3, [ 1, 2, 3, 4, 5, 6, 7 ])));
console.log(JSON.stringify(groupByN(3, [ 1, 2, 3, 4, 5, 6, 7, 8 ])));
The first array gets 2 items because, when i === 1 1%3 will result to 1
Starting from counter 1 could be one solution
data = [1,2,3,4,5,6,7]
var newData = [];
// var setOfThree = []; // not required
var j = 0
newData.push([]);
//pushing at the very begining, this way we won't miss if the data
// is not in groups of 3
for (i = 1; i <= data.length; i++) {
// always updating the final array
newData[j].push(data[i-1]);
if (i % 3 == 0) {
newData.push([]);
j++;
}
}
if (newData[0].length === 0) {
// if the data you received was epmty
newData.pop()
}
console.log(newData)
Here a recursive implementation, with some es6 sugar
var input = [1,2,3,4,5,6,7,8]
function groupByThree([a,b,c,...rest]){
if (rest.length === 0) return [[a,b,c].filter(x => x!==undefined)]
return [[a,b,c]].concat(groupByThree(rest))
}
console.log(groupByThree(input))
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.