I have an array of items :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
// or more items
I have managed to split it into chunks by 3 items per array and pushed them into an array of arrays :
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11] // <== the last one has less items than the others
]
I want to redistribute the items of the last array equally between the previous chunks :
// expected output
[
[1, 2, 3, 10],
[4, 5, 6, 11],
[7, 8, 9],
]
or even more complex like redistrbuting the items at random positions :
// expected output
[
[1, 2, 3, 10],
[4, 5, 6],
[7, 8, 9, 11],
]
so far this is what i have reached :
let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let arrayOfChunks = [];
let amount = 3;
for (let i = 0; i < array.length; i += amount) {
const chunk = array.slice(i, i + amount);
if (chunk.length === amount) {
arrayOfChunks.push(chunk);
} else {
console.log(chunk);
// output [10,11]
}
}
return arrayOfChunks;
I tried making another loop dpending on the arrayOfChunks.length = 3 where I could redistribute the items of the last array evenly into the arrayOfChunks, but sometimes the arrayOfChunks.length = 5 which require another splitting and merging into the previous generated equal chunks.
thanks for your help :)
After chunking the array normally, pop off the last subarray if the chunk number didn't divide the original array length evenly. Then, until that last popped subarray is empty, push items to the other subarrays, rotating indicies as you go.
const evenChunk = (arr, chunkSize) => {
const chunked = [];
for (let i = 0; i < arr.length; i += chunkSize) {
chunked.push(arr.slice(i, i + chunkSize));
}
if (arr.length % chunkSize !== 0) {
const last = chunked.pop();
let i = 0;
while (last.length) {
chunked[i].push(last.shift());
i = (i + 1) % chunked.length;
}
}
console.log(chunked);
};
evenChunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], 7);
evenChunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3);
So, I've the following arrays of odd numbers:
const oddNums = [1, 3, 5, 7, 9];
And I want to fill it with the missing even numbers to obtain the result below:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
I've done it like this and it works fine, but can it be done in a more concise manner, maybe using array methods?
const oddNums = [1, 3, 5, 7, 9];
const nums = [];
for (let i=0; i<oddNums.length; i++) {
nums.push(oddNums[i]);
nums.push(oddNums[i] + 1);
}
console.log(nums);
Note: The odd numbers would always be in sequence but might not begin with 1, for ex: [11, 13, 15] is a valid array. And the output for [11, 13, 15] should be [ 11, 12, 13, 14, 15, 16 ].
The only information you need is the first (odd) number and the size of the input:
const oddNums = [1, 3, 5, 7, 9];
const result = Array.from({length: oddNums.length*2}, (_, i) => i + oddNums[0]);
console.log(result);
Using Array.prototype.flatMap:
const
oddNums = [1, 3, 5, 7, 9],
nums = oddNums.flatMap(n => [n, n + 1]);
console.log(nums);
Using Array.prototype.reduce and Array.prototype.concat:
const
oddNums = [1, 3, 5, 7, 9],
nums = oddNums.reduce((r, n) => r.concat(n, n + 1), []);
console.log(nums);
Using Array.prototype.reduce and Array.prototype.push:
const
oddNums = [1, 3, 5, 7, 9],
nums = oddNums.reduce((r, n) => (r.push(n, n + 1), r), []);
console.log(nums);
Let say there are two array.
let MotherArray = [
[30, 1, 2, 3, 4, 5, 6],
[5, 6, 7, 8, 9],
[7, 8, 9],
];
let arraytoTest = [5,6];
What i want is that i want to return the array if
all the value inside the arraytoTest is included in the MotherArray[i]
I have tried
let MotherArray = [[30, 1, 2, 3, 4, 5, 6],[5, 6, 7, 8, 9],[7, 8, 9],];
let arraytoTest = [5, 6];
let result = MotherArray.includes(arraytoTest)
console.log(result);
But i don't think this is the correct method.
I also find the array.every() but i think my usage is not correct.
What I want is that I want to return MotherArray[0],MotherArray[1] which are [[30, 1, 2, 3, 4, 5, 6],[5, 6, 7, 8, 9]] in this particular example
since 5 and 6 are includes inside these 2 arrays.
You can combine array.filter() with array.every()
let MotherArray = [
[30, 1, 2, 3, 4, 5, 6],
[5, 6, 7, 8, 9],
[7, 8, 9],
];
let arraytoTest = [5,6];
let found = MotherArray.filter(childArray => arraytoTest.every(num => childArray.includes(num)));
console.log(found);
I think this is what you want; A combination of filter on the mother array and every for array you're testing.
let MotherArray = [
[30, 1, 2, 3, 4, 5, 6],
[5, 6, 7, 8, 9],
[7, 8, 9],
];
let arraytoTest = [5,6];
let result = MotherArray.filter(arr => arraytoTest.every(x => arr.indexOf(x)>-1));
console.log(result);
You can use filter and every like below.
Or you can use filter and some with negative condition like filter(x => !arraytoTest.some(y => !x.includes(y))). I think with some it would be efficient because
The some() method executes the callback function once for each element present in the array until it finds the one where callback returns a truthy value.
let MotherArray = [
[30, 1, 2, 3, 4, 5, 6],
[5, 6, 7, 8, 9],
[7, 8, 9],
];
let arraytoTest = [5,6];
let result = MotherArray.filter(x => arraytoTest.every(y => x.includes(y)));
console.log(result);
let result2 = MotherArray.filter(x => !arraytoTest.some(y => !x.includes(y)));
console.log(result2);
i'm trying to understand the following code? and why i get the following output
for (let [i, j, ...x] of [
[2, 3, 4, 5, 10, 11, 12, 13].filter(e => e > 5)
]) {
console.error(x)
}
expected output [10, 11, 12, 13] actual output x = [12, 13]
for(let [i, j, ...x] of [[2, 3, 4, 5, 10, 11, 12, 13].filter(e => e > 5 )]) {
console.error(x)
}
First you need to look at [2, 3, 4, 5, 10, 11, 12, 13].filter(e => e > 5 )
which returns: [10, 11, 12, 13].
Afterwards [i, j, ...x] = [10, 11, 12, 13]will get applied. This means, i = 10, j = 11 and x takes the rest which means x = [12, 13].
We print x and voila, it's [12, 13].
But what about [[10, 11, 12, 13]]? First see the snippet below:
let [k, l, ...y] = [2, 3, 4, 5, 10, 11, 12, 13]
console.log(k,l,y)
let [i, j, ...x] = [[2, 3, 4, 5, 10, 11, 12, 13]]
console.log(i,j,x)
So with a single bracket we get the expected results so why in the given code we have double brackets and it works just fine?
Well, see the following snippet and compare:
for (let [i, j, ...x] of [[2, 3, 4, 5, 10, 11, 12, 13]]) {
console.log(i,j,x)
}
This works just fine but what happens when we loop with single brackets?
for (let [i, j, ...x] of [2, 3, 4, 5, 10, 11, 12, 13]) {
console.log(i,j,x)
}
We get an error! But why?
for...of gets us the inner values of the array of array via its internal iterator. It gets us the 'next' element from our outer array which is our inner array. That's why we only ever got a single iteration in our loop anyway since there is only one 'next'!
On that single element we are iterating we apply the deconstructing syntax and print the result. It's a lot of hot air over nothing, basically.
You Just need to remove i,j
for (let [...x] of [
[2, 3, 4, 5, 10, 11, 12, 13].filter(e => e > 5)
]) {
console.error(x)
}
if you want to get numbers that are larger than 5 , you can use this code
for (let [i, j, ...x] of [
[2, 3, 4, 5, 10, 11, 12, 13].filter(e => e > 5)
]) {
console.error(x)
}
var numberArray= [2, 3, 4, 5, 10, 11, 12, 13];
var numberBiggerThanFive = [];
for(i in numberArray){
if(i > 5 ){
numberBiggerThanFive.push(i)
}
}
console.log(numberBiggerThanFive);
Okay, so I have a multidimensional array that itself contains 9 arrays. Each of these nested arrays contains 10 numeric values. For sake of simplicity, let's say it all looks like this:
var MyArray = [
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]
]
I am trying to write a function that will take the first index of each nested array (in this case, all 1's) and add them together, pushing this value either to an array or an object. Then, I need this function to continue on, adding all the values of the next index, and the next, and so on and so forth. In the end, I should have an array of 10 values (or an object works here as well). The values would be:
1+1+1+1+1+1+1+1+1,
2+2+2+2+2+2+2+2+2,
3+3+3+3+3+3+3+3+3...
...and so on so forth, so that the actual values of the new array would be this:
[9, 18, 27, 36, 45, 54, 63, 72, 81]
The catch here is that I need this to by flexible/dynamic, so that it will work in case MyArray has only 6 arrays, or maybe the nested arrays have only 4 values each. It should work with any amount of nested arrays, each with their own amount of values (though each nested array will contain the SAME amount of values as one another!).
What would be the best way to accomplish this via JavaScript and/or jQuery? Note that I could also have the values output to an object, in this fashion:
{1:9, 2:18, 3:27, 4:36, 5:45, 6:54, 7:63, 8:72, 9:81}
I tried using similar code to this from another StackOverflow thread to get an object, but it is returning
{1:NaN, 2:NaN, 3:NaN, etc.}
That thread can be found here:
Javascript Multidimensional Array: Add Values
I'm using the "underscore" method and the jQuery $.each part of it provided by Otto.
Anyone able to help here??
Something like this
var myData = [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];
var summed = [];
myData[0].forEach(function (arr, index) {
var sum = myData.reduce(function (a, b) {
return a + b[index];
}, 0);
summed.push(sum);
});
console.log(summed);
On jsfiddle
Here is another solution:
var MyArray = [
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]
]
var results= [];
MyArray.map(function(a){
for(var i=0;i<a.length;i++){
if(results.length === a.length){
results[i] = results[i] + a[i];
}else{
results.push(a[i]);
}
}
});
http://jsfiddle.net/uMPAA/
A simple array solution would be the following :
var results= [];
for (var i=0;i<MyArray.length;i++) {
for(var j=0; j<MyArray[i].length; j++) {
if(results[j] == undefined) { results[j] = 0; }
results[j] = results[j]+data[i][j];
}
}
Note the if(results[j]==undefined) line -- this is probably what you didn't do. If you omit that, you get NaN on all lines, since you're adding an undefined value to a number.
Another approach to sum columns in multi-dimensional arrays (based on Lodash 4).
var arrays = [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];
function sum_col(arrays) {
return _.map(_.unzip(arrays), _.sum);
}
console.log(sum_col(arrays));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>