I have an array like so: [1, 2, 3, 4, 5, 6, 7, 9, 10]. I need to chunk it into different size chunks, yet with a simple pattern of: 4, 3, 3, 3, 4, 3, 3, 3 like so:
[
[ // four
1,
2,
3,
4
],
[ // three (1/3)
5,
6,
7
],
[ // three (2/3)
8,
9,
10
],
[ // three (3/3)
11,
12,
13
],
[ // four
14,
15,
16,
17
],
[ // three (1/3)
18,
19,
20
], // and so on..
]
I have tried with this code I have customized:
const arr; // my array of values
const chuncked = arr.reduce((acc, product, i) => {
if (i % 3) {
return acc;
} else if (!didFourWayReduce) {
didFourWayReduce = true;
fourWayReduces++;
if ((fourWayReduces - 1) % 2) { // only make every second a "4 row"
return [...acc, arr.slice(i, i + 3)];
} else {
return [...acc, arr.slice(i, i + 4)];
}
} else {
didFourWayReduce = false;
return [...acc, arr.slice(i, i + 3)];
}
}, []);
And it works, almost, expect that the first chunk of threes (1/3) have the last element of the chunk with 4. So 1 key is repeated every first chunk of three. Like so:
[
[
1,
2,
3,
4
],
[
4, // this one is repeated, and it shouldn't be
5,
6
]
]
You could take two indices, one for the data array and one for sizes. Then slice the array with a given length and push the chunk to the chunks array.
Proceed until end of data.
var data = Array.from({ length: 26 }, (_, i) => i + 1),
sizes = [4, 3, 3, 3],
i = 0,
j = 0,
chunks = [];
while (i < data.length) chunks.push(data.slice(i, i += sizes[j++ % sizes.length]));
console.log(chunks);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const arr = Array.from({ length: 100 }, (_, i) => i);
const copy = [...arr];
const sizes = [4, 3, 3, 3];
const result = [];
let i = 0;
while (i <= arr.length && copy.length) {
result.push(copy.splice(0, sizes[i % sizes.length]));
i++;
}
console.log(result);
A recursive approach is fairly elegant:
const chunks = (xs, [s, ...ss]) =>
xs.length ? [xs .slice (0, s), ... chunks (xs .slice (s), [...ss, s])] : []
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
const sizes = [4, 3, 3, 3]
console .log (chunks (data, sizes))
.as-console-wrapper { max-height: 100% !important; top: 0; }
By replacing [s, ...ss] with [...ss, s], we pass a cycled version of the sizes array, so that for instance, [4, 3, 3, 3] becomes [3, 3, 3, 4]. This makes it easy to parse step-by-step.
Mod operator to check if it should be 4 or 3. Use two arrays just to make it easier (can be done with one)
const groupIt = arr => arr.reduce(({
group,
out
}, v, i) => {
var max = out.length % 4 === 0 ? 4 : 3
group.push(v)
if (group.length === max || i === arr.length - 1) {
out.push(group)
group = []
}
return {
group,
out
}
}, {
group: [],
out: []
}).out
console.log(groupIt([1, 2, 3, 4, 5, 6, 7, 8]))
var test = (new Array(30)).fill(0).map((x,i) => i + 1)
console.log(groupIt(test))
with just one:
const groupIt = arr => arr.reduce((out, v, i) => {
var max = (out.length - 1) % 4 === 0 ? 4 : 3
out[out.length - 1].push(v)
if (out[out.length - 1].length === max) {
out.push([])
}
return out
}, [[]])
console.log(groupIt([1, 2, 3, 4, 5, 6, 7, 8]))
var test = (new Array(30)).fill(0).map((x, i) => i + 1)
console.log(groupIt(test))
This answer is similar to that of Nina Scholz, but uses a for loop, which I personally find more clear.
const arr = Array.from({length: 100}, (_, i) => i + 1);
const sizes = [4, 3, 3, 3];
const result = [];
for (let i = 0, j = 0; i < arr.length; i += sizes[j], j = (j + 1) % sizes.length) {
result.push(arr.slice(i, i + sizes[j]));
}
console.log(result);
Related
i have an array like this
const arr = [1,2,3,4,5,6,7];
i am trying to make sub arrays from this main array so that it will look something like this
const splitArray = [[1,4], [2,5], [3,6], [7]];
what i have tried so far is
const convertToSubArray = (array, chunkSize = 2) => {
let i, j, accum = [];
for (i=0, j=array.length; i<j; i+=chunkSize) {
accum = [...accum, array.slice(i, i+chunkSize)];
}
return accum;
}
But getting this as the output
[
[
1,
2
],
[
3,
4
],
[
5,
6
],
[
7
]
]
How can i achieve this, any help is appreciated
You could take a group of six items as offset and inside of each group of six take another offset build by the half of items (p) in this group.
p indices grouping comment
--- ------------- -------------------------- --------------
3 0 1 2 3 4 5 [[0, 3], [1, 4], [2, 5]]
3 0 1 2 3 4 [[0, 3], [1, 4], [2]]
2 0 1 2 3 [[0, 2], [1, 3]]
2 0 1 2 [[0, 1, 2]] special case
1 0 1 [[0, 1]] "
1 0 [[0]] "
At iterating check if the last index in the group is odd, then take a a single value instead of a pair of values.
const
chunk = array => {
const result = [];
for (let offset = 0; offset < array.length; offset += 6) {
const l = Math.min(array.length - offset, 6);
if (l <= 3) {
result.push(array.slice(offset, offset + 3));
break;
}
for (let i = 0, p = Math.ceil(l / 2); i < p; i++) {
result.push(i + 1 === p && l & 1
? [array[offset + i]]
: [array[offset + i], array[offset + i + p]]
);
}
}
return result;
};
console.log(chunk([1]));
console.log(chunk([1, 2]));
console.log(chunk([1, 2, 3]));
console.log(chunk([1, 2, 3, 4]));
console.log(chunk([1, 2, 3, 4, 5]));
console.log(chunk([1, 2, 3, 4, 5, 6]));
console.log(chunk([1, 2, 3, 4, 5, 6, 7]));
console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8]));
console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
const arr = [1, 2, 3, 4, 5, 6, 7]
const splitArray = [];
for (i = 0; i < arr.length; i++) {
if (arr[i + 3] != undefined) {
splitArray.push([arr[i], arr[i + 3]])
}
}
console.log(splitArray);
My input is an array like so:
[7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7]
I want to group together the numbers and add them, but by neighbors, not by total in the array. So the output would be:
['7:4', '4:2', '5:3', 1, 9, 2, '7:2']
I've attempted a few different methods using reduce, and gotten close but using the built-in Javascript methods I end up counting ALL in the array, not by neighbors.
const firstArray = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7];
const masterArray = [];
const unique = new Set (numberArray); // Set {7, 4, 5, 1, 9, 2, 7}
unique.forEach(u => {
masterArray.push(numberArray.filter(e => e === u));
});
console.log(masterArray);
Set is obviously wrong to use here because that gets the unique values and counts them, but I want to do it by neighbor only. So then I think I should be using a reduce but I run into the same problem.
var test = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7];
console.log(
test.reduce((acc, element) => {
if (acc.length && acc[acc.length - 1].key == element) {
acc[acc.length - 1].count++;
} else {
acc.push({ key: element, count: 1 });
}
return acc;
}, []).map(element => `${element.key}:${element.count}`)
);
So the logic first reduces the number to an array, tracking the last key and count. So long as the key is the same, the count is incremented. Once the key changes, a new object is pushed to start the next run. After the reduce is done, a map is performed to convert the key and counts into the desired strings.
You'll need to keep track of the last number iterated over in a persistent variable, as well as the number of occurrences of the last number which gets incremented:
const arr = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7];
let lastNum = arr[0];
let count = 0;
const results = [];
const doPush = () => {
results.push(
count === 1
? lastNum
: `${lastNum}:${count}`
);
};
for (const num of arr) {
if (num !== lastNum) {
doPush();
lastNum = num;
count = 1;
} else count++;
}
doPush();
console.log(results);
You could reduce the array by having a look to the last value a the index before the actual value.
const
array = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7],
result = array.reduce((r, value, i, { [i - 1]: last }) => {
let count = 0;
if (last === value) count = (+r.pop().toString().split(':')[1] || 0) + 1;
return [...r, count ? `${value}:${count}`: value];
}, []);
console.log(result);
Here is a solution that uses a recursive function to group the neighbors, and then Array.prototype.map() to format with a colon:
const arr = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7],
output = (function groupNeighbors([first, ...rest], output = [], last) {
last === first ? output[output.length - 1].push(first) : output.push([first]);
return rest.length ? groupNeighbors(rest, output, first) : output;
})(arr).map(({ [0]: num, length }) => length > 1 ? [num, length].join(':') : num);
console.log(output);
As it uses recursion, it is limited in terms of input array length, you can find the stack size limits per browser here: Browser Javascript Stack size limit
Variant with Array.prototype.reduce() (slightly shortest, no recursion, unlimited input length):
const arr = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7],
output = arr
.reduce(
(acc, cur, i, { [i - 1]: last }) =>
(cur === last ? acc[acc.length - 1].push(cur) : acc.push([cur])) && acc,
[]
)
.map(({ [0]: num, length }) => length > 1 ? [num, length].join(':') : num);
console.log(output);
Yes, the proper choice here is reduce:
const countDuplicatesQuantity = (arr) => {
const duplicatesObj = arr.reduce((duplicates, item) => {
duplicates[item] = duplicates[item] ? duplicates[item] + 1 : 1;
return duplicates;
}, {});
return Object.keys(duplicatesObj).map(
(key) => `${key}:${duplicatesObj[key]}`,
);
};
You wanted reduce, how about twice? :) (I don't know if I've done something stupid here.)
First reduce finds where the values change in the array, second uses that to build new array.
const firstArray = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7];
const masterArray = firstArray
.reduce((acc, v, i, all) => all[i + 1] !== v ? acc.concat(i) : acc, [])
.reduce(
(acc, v, i, all) => {
const range = v - (i === 0 ? -1 : all[i - 1]);
return acc.concat(range > 1 ? firstArray[v] + ':' + range : firstArray[v]);
}, []
);
console.log(masterArray);
Using array.reduce:
const firstArray = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7];
var newArray = [];
var count = 0;
firstArray.reduce((acc, cur, index) => {
if (acc == cur) {
count++;
}
if (acc != cur || index+1 == firstArray.length) {
newArray.push(acc + ":" + count);
count=1
}
return cur;
})
console.log(newArray);
my way ... I felt that there could be "simpler"
const firstArray = [7, 7, 7, 7, 4, 4, 5, 5, 5, 1, 9, 2, 7, 7];
const result = firstArray.reduceRight((r,v)=>
{
let [n,c] = !r.length ? [-v,0] : isNaN(r[0]) ? r[0].split(':') : [r[0],1]
if (n==v) r[0] = `${n}:${++c}`
else r.unshift(v)
return r
},[])
console.log( JSON.stringify(result) )
// [ "7:4", "4:2", "5:3", 1, 9, 2, "7:2" ]
.as-console-wrapper { max-height: 100% !important; top: 0; }
i want to specify how many 1 elements I want to remove within that array:
const array = [1, 1, 2, 3, 1, 5];
I tried like that:
const array = [1, 1, 2, 3, 1, 5];
const i = array.indexOf(1);
if (i > - 1) {
array.splice(i, 1);
}
but this remove just first element 1 in array
Try this, Array.prototype.filter will create a new array and will not mutate or change the array on which it is called.
const arr = [1, 1, 2, 3, 1, 5];
const newArr = arr.filter((item) => item !== 1);
console.log(newArr);
const removeArrayItem = (array, item) => array.filter((i) => i !== item);
console.log(removeArrayItem([1, 2, 2, 2, 2, 3, 4, 5], 2));
console.log(removeArrayItem([1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 8], 8));
// or using reduce
const removeDuplicateItem = (arr, itemToRemove, count) =>
arr.reduce(
(accumulator, currentItem) => {
if (
accumulator.count &&
accumulator.count > 0 &&
currentItem === itemToRemove
) {
accumulator.count -= 1;
return accumulator;
}
accumulator.result.push(currentItem);
return accumulator;
},
{
count,
result: [],
},
).result;
// This will remove 5 number twos
console.log(removeDuplicateItem([1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 8], 2, 5));
You need to iterate the count and check if an item is available for removing.
function remove(array, value, count) {
while (count--) {
const i = array.indexOf(value);
if (i === -1) break;
array.splice(i, 1);
}
}
const array = [1, 1, 2, 3, 1, 5];
remove(array, 1, 2);
console.log(array);
If you want to remove only the amount of 1s you specify,
You can use a for loop with 2 conditions,
one for array size and the second for the number of times 1 was found and removed.
const array = [1, 1, 2, 3, 1, 5];
const counter = 2;
for(let i = 0 , n = 0; i < array.length && n < counter; i++) {
if(array[i] === 1) {
array.splice(i, 1);
i -= 1;
n++;
}
}
console.log(array);
You can use a while loop to remove N number of elements with this value:
const removeNInstancesOfX = (array, n, x) => {
let i = null, count = 0, arr = [...array];
while ((i = arr.indexOf(1)) !== -1 && count++ < n)
arr.splice(i, 1);
return arr;
}
let array = [1, 1, 2, 3, 1, 5];
array = removeNInstancesOfX(array, 2, 1);
console.log(...array);
I have an array like so: [1, 2, 3, 4, 5, 6, 7, 9, 10]. I need to chunk it into different size chunks, yet with a simple pattern of: 4, 3, 3, 3, 4, 3, 3, 3 like so:
[
[ // four
1,
2,
3,
4
],
[ // three (1/3)
5,
6,
7
],
[ // three (2/3)
8,
9,
10
],
[ // three (3/3)
11,
12,
13
],
[ // four
14,
15,
16,
17
],
[ // three (1/3)
18,
19,
20
], // and so on..
]
I have tried with this code I have customized:
const arr; // my array of values
const chuncked = arr.reduce((acc, product, i) => {
if (i % 3) {
return acc;
} else if (!didFourWayReduce) {
didFourWayReduce = true;
fourWayReduces++;
if ((fourWayReduces - 1) % 2) { // only make every second a "4 row"
return [...acc, arr.slice(i, i + 3)];
} else {
return [...acc, arr.slice(i, i + 4)];
}
} else {
didFourWayReduce = false;
return [...acc, arr.slice(i, i + 3)];
}
}, []);
And it works, almost, expect that the first chunk of threes (1/3) have the last element of the chunk with 4. So 1 key is repeated every first chunk of three. Like so:
[
[
1,
2,
3,
4
],
[
4, // this one is repeated, and it shouldn't be
5,
6
]
]
You could take two indices, one for the data array and one for sizes. Then slice the array with a given length and push the chunk to the chunks array.
Proceed until end of data.
var data = Array.from({ length: 26 }, (_, i) => i + 1),
sizes = [4, 3, 3, 3],
i = 0,
j = 0,
chunks = [];
while (i < data.length) chunks.push(data.slice(i, i += sizes[j++ % sizes.length]));
console.log(chunks);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const arr = Array.from({ length: 100 }, (_, i) => i);
const copy = [...arr];
const sizes = [4, 3, 3, 3];
const result = [];
let i = 0;
while (i <= arr.length && copy.length) {
result.push(copy.splice(0, sizes[i % sizes.length]));
i++;
}
console.log(result);
A recursive approach is fairly elegant:
const chunks = (xs, [s, ...ss]) =>
xs.length ? [xs .slice (0, s), ... chunks (xs .slice (s), [...ss, s])] : []
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
const sizes = [4, 3, 3, 3]
console .log (chunks (data, sizes))
.as-console-wrapper { max-height: 100% !important; top: 0; }
By replacing [s, ...ss] with [...ss, s], we pass a cycled version of the sizes array, so that for instance, [4, 3, 3, 3] becomes [3, 3, 3, 4]. This makes it easy to parse step-by-step.
Mod operator to check if it should be 4 or 3. Use two arrays just to make it easier (can be done with one)
const groupIt = arr => arr.reduce(({
group,
out
}, v, i) => {
var max = out.length % 4 === 0 ? 4 : 3
group.push(v)
if (group.length === max || i === arr.length - 1) {
out.push(group)
group = []
}
return {
group,
out
}
}, {
group: [],
out: []
}).out
console.log(groupIt([1, 2, 3, 4, 5, 6, 7, 8]))
var test = (new Array(30)).fill(0).map((x,i) => i + 1)
console.log(groupIt(test))
with just one:
const groupIt = arr => arr.reduce((out, v, i) => {
var max = (out.length - 1) % 4 === 0 ? 4 : 3
out[out.length - 1].push(v)
if (out[out.length - 1].length === max) {
out.push([])
}
return out
}, [[]])
console.log(groupIt([1, 2, 3, 4, 5, 6, 7, 8]))
var test = (new Array(30)).fill(0).map((x, i) => i + 1)
console.log(groupIt(test))
This answer is similar to that of Nina Scholz, but uses a for loop, which I personally find more clear.
const arr = Array.from({length: 100}, (_, i) => i + 1);
const sizes = [4, 3, 3, 3];
const result = [];
for (let i = 0, j = 0; i < arr.length; i += sizes[j], j = (j + 1) % sizes.length) {
result.push(arr.slice(i, i + sizes[j]));
}
console.log(result);
How to use recursive function to reordered array with even indices first and then odd indices?
For example:
input: [5, 2, 4, 9]
output: [5, 4, 2, 9]
I want to improve this code.
let arrayB = [],
arrayOdd = [],
arrayEven = [];
let i = 0;
const reorder = (arrayA) => {
if (arrayA.length >= 2) {
if (i < arrayA.length) {
i === 0 || i % 2 === 0 ? arrayEven.push(arrayA[i]) : arrayOdd.push(arrayA[i]);
i++;
arrayB = [...arrayEven, ...arrayOdd];
reorder(arrayA);
} else {
arrayOdd = [];
arrayEven = [];
i = 0;
}
return arrayB;
}
}
console.log(reorder([4, 8, 12, 16]));
console.log(reorder([1, 2, 3, 4, 5, 6, 7, 8, 9]));
No idea why you'd need a recursive function. This should do it:
const reorder = (a) => [...a.filter((_, i) => !(i % 2)), ...a.filter((_, i) => i % 2)];
console.log(reorder([4, 8, 12, 16]));
console.log(reorder([1, 2, 3, 4, 5, 6, 7, 8, 9]));
Because you stipulate your in comments that you have instructions to use a recursive function with minimum space requirements, here's a solution that uses Array.prototype.splice() to modify the array in place:
const reorder = (a, offset = 2) => {
if (offset < a.length) {
a.splice(offset / 2, 0, ...a.splice(offset, 1));
return reorder(a, offset + 2);
}
return a;
};
console.log(reorder([4, 8, 12, 16]));
console.log(reorder([1, 2, 3, 4, 5, 6, 7, 8, 9]));
Note that, like the Array.prototype.sort() function, the above function only returns the array for convenience. No new array is created.
I think you can do this by using sort method of array. but for that you might need to use map to get index and then converting back to values.
const isOdd = n => n % 2 === 1;
const reorder = input => input.map((value, index) => ({index, value})).sort((a,b) =>
isOdd(a.index) ? 1 : isOdd(b.index) ? -1 : 0).map(({value}) => value);
console.log(reorder([4, 8, 12, 16]));
console.log(reorder([1, 2, 3, 4, 5, 6, 7, 8, 9]));
Avoid using global variables.
const reorder = (arrayA, arrayOdd = [], arrayEven = []) => {
if (arrayA.length >= 1)
arrayEven.push(arrayA.shift());
if (arrayA.length >= 1) {
arrayOdd.push(arrayA.shift());
reorder(arrayA, arrayOdd, arrayEven);
}
return [...arrayEven, ...arrayOdd];
}
console.log(reorder([4, 8, 12, 16]));
console.log(reorder([1, 2, 3, 4, 5, 6, 7, 8, 9]));
I don't know why you would choose to do this recursively, but this seems to do it:
const evenThenOdd = ([e = undefined, o = undefined, ...xs], evens = [], odds = []) =>
e == undefined
? [...evens, ...odds]
: o == undefined
? evenThenOdd ( xs, [...evens, e] [odds] )
: evenThenOdd ( xs, [...evens, e], [...odds, o] )
console .log (
evenThenOdd ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
//~> [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
)
We keep evens and odds arrays of values. The base case, when there is nothing left in the array, is to just concatenate those two arrays. The other two cases are when there is one item left and where there are two or more items left. If the undefined values are legitimate elements of your array you want to handle, then add const None = Symbol(), and replace each undefined with None.
A cleaner, non-recursive solution might look like this:
const evenThenOdd = (xs) => [
...xs.filter((_, i) => i % 2 == 0),
...xs.filter((_, i) => i % 2 == 1),
]