Related
I am trying to create STL files using vtk.js of a vtkTubeFilter polyData.
The error am getting is:
Error: Unsupported strips
at Object.writeSTL
The code I'm using to get STL data:
const data = vtkSTLWriter.writeSTL(
polyData,
'ascii',
transform.getMatrix()
);
where, transform is a vtkTransform object and polyData is the polyData obtained from getOutputData() on vtkTubeFilter source.
As per vtk.js source code for writeSTL function, the number of strips must be 0 or null.
I checked on the polyData object, it has an array of length 140 for getStrips().getData().
let strips = newPolyData.getStrips().getData();
console.log(strips.length);
console.log(strips)
140
Uint32Array(140) [6, 1, 0, 21, 20, 41, 40, 6, 2, 1, 22, 21, 42, 41, 6, 3, 2, 23, 22, 43, 42, 6, 4, 3, 24, 23, 44, 43, 6, 5, 4, 25, 24, 45, 44, 6, 6, 5, 26, 25, 46, 45, 6, 7, 6, 27, 26, 47, 46, 6, 8, 7, 28, 27, 48, 47, 6, 9, 8, 29, 28, 49, 48, 6, 10, 9, 30, 29, 50, 49, 6, 11, 10, 31, 30, 51, 50, 6, 12, 11, 32, 31, 52, 51, 6, 13, 12, 33, 32, 53, 52, 6, 14, 13, 34, 33, 54, 53, 6, 15, …]
How can fix/debug this error, and still write it to STL file. Or, what am I doing wrong?
i have an array and i want convert this array to separate object , glad to help me, thanks
array = ["January", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, "February", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
i want
result = [
{
month:'January',
days:[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
},
{
month:'February',
days:[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
}
]
First find out the indexes for strings in array, so that you can split array.
const process = (arr) => {
const result = [];
let last = -1;
for (let i = 0; i <= arr.length; i++) {
if (typeof arr[i] === "string" || !(i in arr)) {
if (last !== -1) {
result.push([last, i]);
}
last = i;
}
}
return result.map(([start, end]) => ({
month: arr[start],
days: arr.slice(start + 1, end),
}));
};
const array = ["January", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, "February", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
console.log(process(array));
I want to calculate the total number of cases, recovered and deaths as whole from a JSON object. Suppose for example: the date 3/27/20 should have the value of the computed the total number of cases, recovered and deaths for the remaining countries. How can I do in this JavaScript, and return me an object of computed values for each date, removing the countries, province? I have the following JSON object:
{
"country": "Afghanistan",
"province": null,
"timeline": {
"cases": {
"3/27/20": 110,
"3/28/20": 110,
"3/29/20": 120,
"3/30/20": 170,
"3/31/20": 174,
"4/1/20": 237,
"4/2/20": 273,
"4/3/20": 281,
"4/4/20": 299,
"4/5/20": 349,
"4/6/20": 367,
"4/7/20": 423,
"4/8/20": 444,
"4/9/20": 484,
"4/10/20": 521
},
"deaths": {
"3/27/20": 4,
"3/28/20": 4,
"3/29/20": 4,
"3/30/20": 4,
"3/31/20": 4,
"4/1/20": 4,
"4/2/20": 6,
"4/3/20": 6,
"4/4/20": 7,
"4/5/20": 7,
"4/6/20": 11,
"4/7/20": 14,
"4/8/20": 14,
"4/9/20": 15,
"4/10/20": 15
},
"recovered": {
"3/27/20": 2,
"3/28/20": 2,
"3/29/20": 2,
"3/30/20": 2,
"3/31/20": 5,
"4/1/20": 5,
"4/2/20": 10,
"4/3/20": 10,
"4/4/20": 10,
"4/5/20": 15,
"4/6/20": 18,
"4/7/20": 18,
"4/8/20": 29,
"4/9/20": 32,
"4/10/20": 32
}
}
},
{
"country": "Albania",
"province": null,
"timeline": {
"cases": {
"3/27/20": 186,
"3/28/20": 197,
"3/29/20": 212,
"3/30/20": 223,
"3/31/20": 243,
"4/1/20": 259,
"4/2/20": 277,
"4/3/20": 304,
"4/4/20": 333,
"4/5/20": 361,
"4/6/20": 377,
"4/7/20": 383,
"4/8/20": 400,
"4/9/20": 409,
"4/10/20": 416
},
"deaths": {
"3/27/20": 8,
"3/28/20": 10,
"3/29/20": 10,
"3/30/20": 11,
"3/31/20": 15,
"4/1/20": 15,
"4/2/20": 16,
"4/3/20": 17,
"4/4/20": 20,
"4/5/20": 20,
"4/6/20": 21,
"4/7/20": 22,
"4/8/20": 22,
"4/9/20": 23,
"4/10/20": 23
},
"recovered": {
"3/27/20": 31,
"3/28/20": 31,
"3/29/20": 33,
"3/30/20": 44,
"3/31/20": 52,
"4/1/20": 67,
"4/2/20": 76,
"4/3/20": 89,
"4/4/20": 99,
"4/5/20": 104,
"4/6/20": 116,
"4/7/20": 131,
"4/8/20": 154,
"4/9/20": 165,
"4/10/20": 182
}
}
}
var a = [{"country":"Afghanistan","province":null,"timeline":{"cases":{"3/27/20":110,"3/28/20":110,"3/29/20":120,"3/30/20":170,"3/31/20":174,"4/1/20":237,"4/2/20":273,"4/3/20":281,"4/4/20":299,"4/5/20":349,"4/6/20":367,"4/7/20":423,"4/8/20":444,"4/9/20":484,"4/10/20":521},"deaths":{"3/27/20":4,"3/28/20":4,"3/29/20":4,"3/30/20":4,"3/31/20":4,"4/1/20":4,"4/2/20":6,"4/3/20":6,"4/4/20":7,"4/5/20":7,"4/6/20":11,"4/7/20":14,"4/8/20":14,"4/9/20":15,"4/10/20":15},"recovered":{"3/27/20":2,"3/28/20":2,"3/29/20":2,"3/30/20":2,"3/31/20":5,"4/1/20":5,"4/2/20":10,"4/3/20":10,"4/4/20":10,"4/5/20":15,"4/6/20":18,"4/7/20":18,"4/8/20":29,"4/9/20":32,"4/10/20":32}}},{"country":"Albania","province":null,"timeline":{"cases":{"3/27/20":186,"3/28/20":197,"3/29/20":212,"3/30/20":223,"3/31/20":243,"4/1/20":259,"4/2/20":277,"4/3/20":304,"4/4/20":333,"4/5/20":361,"4/6/20":377,"4/7/20":383,"4/8/20":400,"4/9/20":409,"4/10/20":416},"deaths":{"3/27/20":8,"3/28/20":10,"3/29/20":10,"3/30/20":11,"3/31/20":15,"4/1/20":15,"4/2/20":16,"4/3/20":17,"4/4/20":20,"4/5/20":20,"4/6/20":21,"4/7/20":22,"4/8/20":22,"4/9/20":23,"4/10/20":23},"recovered":{"3/27/20":31,"3/28/20":31,"3/29/20":33,"3/30/20":44,"3/31/20":52,"4/1/20":67,"4/2/20":76,"4/3/20":89,"4/4/20":99,"4/5/20":104,"4/6/20":116,"4/7/20":131,"4/8/20":154,"4/9/20":165,"4/10/20":182}}}]
var result = a.map(function(e){return {cases: e.timeline.cases, deaths: e.timeline.deaths, recovered: e.timeline.recovered};}).reduce(function(acc, e){
Object.keys(e).forEach(function(t){
Object.keys(e[t]).forEach(function(d){
acc[t][d] = (acc[t][d] || 0) + e[t][d];
});
});
return acc;
}, {deaths: {}, recovered: {}, cases: {}});
console.log(result);
I have a js array like this.
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ]
var combination_before = [ 20,7,23, 19, 10, 15, 25, 8, 13 ];
These are the results of choosing seven out of nine.
I would like to return some elements only when sum of the elements is 100.
how do I make reducer if condition?
you can use filter for filtering your array and reduce for sum your nested arrays, and check in your filter which array sum is equal to 100
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ];
const result = test_arr.filter(arr => arr.reduce((a, b) => a + b, 0) == 100);
console.log(result);
const test_arr = [ [ 20, 7, 23, 19, 10, 15, 25 ],
[ 20, 7, 23, 19, 10, 15, 8 ],
[ 20, 7, 23, 19, 10, 15, 13 ],
[ 20, 7, 23, 19, 10, 25, 8 ],
[ 20, 7, 23, 19, 10, 25, 13 ],
[ 20, 7, 23, 19, 10, 8, 13 ],
[ 20, 7, 23, 19, 15, 25, 8 ],
[ 20, 7, 23, 19, 15, 25, 13 ],
[ 20, 7, 23, 19, 15, 8, 13 ],
[ 20, 7, 23, 19, 25, 8, 13 ],
[ 20, 7, 23, 10, 15, 25, 8 ],
[ 20, 7, 23, 10, 15, 25, 13 ],
[ 20, 7, 23, 10, 15, 8, 13 ],
[ 20, 7, 23, 10, 25, 8, 13 ],
[ 20, 7, 23, 15, 25, 8, 13 ],
[ 20, 7, 19, 10, 15, 25, 8 ],
[ 20, 7, 19, 10, 15, 25, 13 ],
[ 20, 7, 19, 10, 15, 8, 13 ],
[ 20, 7, 19, 10, 25, 8, 13 ],
[ 20, 7, 19, 15, 25, 8, 13 ],
[ 20, 7, 10, 15, 25, 8, 13 ],
[ 20, 23, 19, 10, 15, 25, 8 ],
[ 20, 23, 19, 10, 15, 25, 13 ],
[ 20, 23, 19, 10, 15, 8, 13 ],
[ 20, 23, 19, 10, 25, 8, 13 ],
[ 20, 23, 19, 15, 25, 8, 13 ],
[ 20, 23, 10, 15, 25, 8, 13 ],
[ 20, 19, 10, 15, 25, 8, 13 ],
[ 7, 23, 19, 10, 15, 25, 8 ],
[ 7, 23, 19, 10, 15, 25, 13 ],
[ 7, 23, 19, 10, 15, 8, 13 ],
[ 7, 23, 19, 10, 25, 8, 13 ],
[ 7, 23, 19, 15, 25, 8, 13 ],
[ 7, 23, 10, 15, 25, 8, 13 ],
[ 7, 19, 10, 15, 25, 8, 13 ],
[ 23, 19, 10, 15, 25, 8, 13 ] ];
var resultArr = [];
for (var i = 0; i < test_arr.length; i++){
temp = test_arr[i].reduce(getSum)
if (temp == 100) {
resultArr.push(test_arr[i]);
}
}
function getSum(total, num) {
return total + num;
}
console.log(resultArr)
You could take a different approach at the moment, where you take all combination and return only the ones who match the conditions by length and sum.
function getCombinations(array, length, sum) {
function iter(array, temp) {
if (temp.length === length) {
if (temp.reduce((a, b) => a + b) === sum) {
result.push(temp);
}
return;
}
if (!array.length || array.length + temp.length < length) {
return;
}
iter(array.slice(1), temp.concat(array[0]));
iter(array.slice(1), temp);
}
var result = [];
iter(array, []);
return result;
}
var array = [20, 7, 23, 19, 10, 15, 25, 8, 13];
console.log(getCombinations(array, 7, 100));
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have a challenge that I have been battling with for some time now. It's about replacing all the elements in an array that is in a grid form, but my solution is only replacing selected element of its choice not as I intended.
In this challenge, I want to replace the integer value that is divisible by two with the string "even" while the rest replaced with the string "odd".
/*
* - The numbers variable is an array of arrays.
* - a nested for loop to cycle through numbers.
* - it convert each even number to the string "even"
* - and Convert each odd number to the string "odd"
*/
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for(var row=0; row<myNumbers.length; row++) {
for(var column=0;column<myNumbers[row].length;column++) {
if(myNumbers[column]%2===0){
myNumbers[column].splice(column,1,"even");
}else{
myNumbers[column].splice(column,1,"odd");
}
console.log(myNumbers[row][column]);
}
}
Code Output:
odd
12
23
12
45
45
78
66
223
3
34
odd
1
553
23
4
66
23
4
55
67
56
odd
553
44
55
5
428
452
3
12
31
55
odd
79
44
674
224
4
21
4
2
3
52
odd
51
44
1
67
5
5
65
4
5
5
odd
5
43
23
4424
74
532
6
7
35
17
odd
43
43
66
53
6
89
10
23
52
111
odd
109
80
67
6
53
537
2
168
16
2
odd
8
76
7
9
6
3
73
77
100
56
odd
You used the wrong variable for the row index
myNumbers[column]
//needs to be
myNumbers[row]
Also your if condition is using the wrong row index, and trying to compare against the whole array instead of the value in the array
if(myNumbers[column]%2===0)
//needs to be
if(myNumbers[row][column]%2===0)
Demo
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for (var row = 0; row < myNumbers.length; row++) {
for (var column = 0; column < myNumbers[row].length; column++) {
if (myNumbers[row][column] % 2 === 0) {
myNumbers[row].splice(column, 1, "even");
} else {
myNumbers[row].splice(column, 1, "odd");
}
}
}
console.log(myNumbers);
Probably easier to use a nested map instead:
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = myNumbers.map(row => row.map(num =>
num % 2 === 0
? 'even'
: 'odd'
));
console.log(output);
Achieving the same thing with a for loop is much more verbose and confusing, and shouldn't be done in most cases (array methods have better abstraction and don't require manual iteration), but if necessary:
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
const output = [];
for (let rowIndex = 0; rowIndex < myNumbers.length; rowIndex++) {
const row = myNumbers[rowIndex];
const newRow = [];
for (let colIndex = 0; colIndex < row.length; colIndex++) {
const num = row[colIndex];
newRow.push(num % 2 === 0 ? 'even' : 'odd');
}
output.push(newRow);
}
console.log(output);
Why not simply set the value of myNumbers[row][column] instead of using splice?
var myNumbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for(var row=0; row<myNumbers.length; row++) {
for(var column=0;column<myNumbers[row].length;column++) {
if(myNumbers[row][column]%2===0) {
myNumbers[row][column] = "even";
} else{
myNumbers[row][column] = "odd";
}
console.log(myNumbers[row][column]);
}
}
In your inner loop you need to access the values using both indexes (i.e. myNumbers[row][column]). As it stands, you are only using the column index and therefore splicing values into the array containing the rows.