I have a cmyk array which is :
JavaScript
x
2
1
cmykColor = [[0,1,0,1],[0,0,1,1],[0,0,0,1]]
2
and the other one is the number array which is:
JavaScript
1
2
1
arrGeo = [4,2,1]
2
I want to have an array which has iterative cmyk respect to arrGeo numbers which means:
JavaScript
1
6
1
cmykArray = [
2
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,],
3
[0,0,1,1,0,0,1,1],
4
[0,0,0,1]
5
]
6
and this is my code to make it :
JavaScript
1
12
12
1
for (let i = 0; i < arrGeo.length; i++) {
2
var cmyk = [];
3
var cmykArray = [];
4
for (let j = 0; j < arrGeo[i].length; j++) {
5
for (let k = 0; k < 4; k++) {
6
cmyk = cmyk.concat(cmykColor[i][k]);
7
console.log(hex[i]);
8
}
9
}
10
cmykArr.push(cmyk);
11
}
12
Advertisement
Answer
You could map with another loop for the wanted count.
JavaScript
1
11
11
1
const
2
cmykColor = [[0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]],
3
arrGeo = [4, 2, 1],
4
result = cmykColor.map((a, i) => {
5
let c = arrGeo[i],
6
t = [];
7
while (c--) t.push(a);
8
return t;
9
});
10
11
console.log(result);
JavaScript
1
1
1
.as-console-wrapper { max-height: 100% !important; top: 0; }