Error: Unsupported strips at Object.writeSTL in vtk.js - javascript

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?

Related

convert array to separate object array

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));

Join first two elements and create new array?

After playing around with the join() and slice() functions for hours, I have just found that you can't use either function on complex arrays. So I've come to get some help here.
I'm trying to get my data below:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
To look like this (output):
var result = [
["North: Tennis", 37, 25, 11, 9, 42, 13],
["East: Football", 41, 2, 3, 26, 47, 21],
["South: Rugby", 7, 22, 35, 45, 11, 46],
["West: Rugby", 30, 21, 44, 23, 4, 47],
["North East: Football", 35, 27, 12, 39, 34, 13],
["North West: Football", 23, 4, 41, 35, 9, 47]
];
Any help would be appreciated
As you want to turn one array into another array, it makes sense to .map the original array to another array by taking the first two elements of each subarray and concatenating them together, then building a new subarray by adding the rest of the elements (which we get with the rest parameter during the array destructuring) to a new array that contains the joined string.
let data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
let result = data.map(([s1, s2, ...rest]) => [`${s1}: ${s2}`, ...rest]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
References:
Array.prototype.map()
Array destructuring
Template literals
If I understand your question correctly, you are looking for the following output. I've used JavaScript's .map() function to restructure the data slightly. More information on .map here.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
const formattedData = data.map(row => {
return [
[row[0] + ': ' + row[1]],
row[2],
row[3],
row[4],
row[5],
row[6],
row[7]
];
});
console.log(formattedData);
I'd write your code.
var data = [
["North", "Tennis", 37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
// Final resulting array is created by concatenating the first and
// second index elements of each inner array of data. The rest data
// is being copied as it is using slice and spread operator.
const result = data.reduce(
(mem, cur) => [...mem, [`${cur[0]}: ${cur[1]}`, ...cur.slice(2)]],
[]
);
console.log(result);
You could use map method. It takes a callback which is executed for every element. It then returns a new array with the new data in each index. For example:
var data = [
["North", "Tennis",37, 25, 11, 9, 42, 13],
["East", "Football", 41, 2, 3, 26, 47, 21],
["South", "Rugby", 7, 22, 35, 45, 11, 46],
["West", "Rugby", 30, 21, 44, 23, 4, 47],
["North East", "Football", 35, 27, 12, 39, 34, 13],
["North West", "Football", 23, 4, 41, 35, 9, 47]
];
newData = data.map((el, i, arr) => {
el[0] = `${el[0]}: ${el[1]}` // reassinging el[0]
el.splice(1, 1); // removing 2 array element
});

Replacing all elements in a nested Array

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.

how to create a new array form nested arrays using nested loops JS

I have a nested arrays, I need to create two new arrays one for the even numbers and the other is for odd numbers using nested for loop.
I used push method but the result was that every number was in a separate array while I need all even numbers to be set in a single array and the same for the odd numbers.
Here is my code:
var numbers = [
[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 x = 0; x < numbers.length; x++) {
for (var y = 0; y < numbers[x].length; y++) {
if (numbers[x][y] % 2 === 0) {
var even = [];
even.push(numbers[x][y]);
} else {
var odd = [];
odd.push(numbers[x][y]);
}
console.log(odd);
}
You have to merge all the nested array in a new array and then you can find all the odd and even numbers separately as below
var numbers = [
[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]
];
var merged = [].concat.apply([], numbers);
var evenValues = [];
var oddValues = [];
for (var i = 0; i < merged.length; i++) {
if ((merged[i] % 2) != 1) {
evenValues.push(merged[i]);
} else {
oddValues.push(merged[i]);
}
}
console.log(evenValues);
console.log(oddValues);
var odd = [], even= [];
for (var x = 0; x < numbers.length; x++) {
for (var y = 0; y < numbers[x].length; y++) {
if (numbers[x][y] % 2 === 0) {
even.push(numbers[x][y]);
} else {
odd.push(numbers[x][y]);
}
console.log(odd);
}
Just put even and odd variables outside the loop

How to add each set of numbers within an array of arrays? (javascript)

I'm struggling to design a loop that will cycle through an array of arrays and add each number together, work out the average then output to the console.
Here is my code;
var data = [
[3, 6, 14, 17, 30, 40, 44, 66, 69, 84, 92, 95],
[100, 17, 26, 28, 29, 34, 38, 59, 78, 82, 84, 93],
[6, 12, 22, 25, 35, 44, 45, 57, 60, 61, 78, 80],
[6, 11, 14, 19, 33, 50, 57, 58, 61, 88, 89, 97],
[6, 13, 23, 28, 39, 44, 50, 55, 58, 72, 80, 88],
[6, 8, 22, 26, 48, 50, 55, 65, 77, 84, 93, 99]
]
var calcTotal, arrayTotal, totalSum;
calcTotal = [];
arrayTotal = [];
totalSum = [];
arrayTotal.push(data[0])
totalSum = data[0].reduce(function(a, b) {
return a + b;
});
calcTotal.push(totalSum)
console.log(Math.round(totalSum / 12))
http://plnkr.co/edit/Ses4XApKEdo2CCZmsis7?p=preview
So far I have it working to display just one result, Ideally I would output the average from each array when added together in a single array to the console.
I've been playing around with for/forEach loops but can't seem to crack it, if anyone can offer some help/advice?
Thanks
It's pretty easy: you can write two functions, add and average, use Array#map and Array#reduce:
var data = [
[3, 6, 14, 17, 30, 40, 44, 66, 69, 84, 92, 95],
[100, 17, 26, 28, 29, 34, 38, 59, 78, 82, 84, 93],
[6, 12, 22, 25, 35, 44, 45, 57, 60, 61, 78, 80],
[6, 11, 14, 19, 33, 50, 57, 58, 61, 88, 89, 97],
[6, 13, 23, 28, 39, 44, 50, 55, 58, 72, 80, 88],
[6, 8, 22, 26, 48, 50, 55, 65, 77, 84, 93, 99]
];
function add(a, b) {
return a + b;
}
function average(list) {
return list.reduce(add) / list.length;
}
document.body.textContent = data.map(average);
So as far as I understand you need to display to the console average of each row right?
You've done pretty good job with single line it just needed to be packed with forEach, here's working fiddle: https://jsfiddle.net/enowacki/dhdc1ztc/2/
const data = [
[3, 6, 14, 17, 30, 40, 44, 66, 69, 84, 92, 95],
[100, 17, 26, 28, 29, 34, 38, 59, 78, 82, 84, 93],
[6, 12, 22, 25, 35, 44, 45, 57, 60, 61, 78, 80],
[6, 11, 14, 19, 33, 50, 57, 58, 61, 88, 89, 97],
[6, 13, 23, 28, 39, 44, 50, 55, 58, 72, 80, 88],
[6, 8, 22, 26, 48, 50, 55, 65, 77, 84, 93, 99]
];
const result = data.map((arr) => {
const rowSum = arr.reduce((prev, curr) => prev + curr);
const rowCount = arr.length;
const avg = Math.round(rowSum / rowCount);
return avg;
});
console.log(result);
I've extracted some additional variables so you can clearly see what's going on feel free to omit them if not needed.
Flatten:
var flat = [].concat.apply([], data);
Sum:
var sum = flat.reduce((a, b) => a+b, 0);
console.log("Avg:" + Math.round(sum / flat.length);

Categories

Resources