Related
I am trying to convert a metatag to plain text using the Buffer.from and .toString(). My goal is to display an array of the keywords using the .map method within my main function. But I notice some strange behavior. My map function isn't displaying the keywords correctly.
Keywords: [79, 0, 98, 0, 115, 0, 116, 0, 59, 0, 80, 0, 101, 0, 114, 0, 115, 0, 111, 0, 110, 0, 59, 0]
const buffer = Buffer.from(Keywords);
buffer:
{"data": [79, 0, 98, 0, 115, 0, 116, 0, 59, 0, 80, 0, 101, 0, 114, 0, 115, 0, 111, 0, 110, 0, 59, 0], "type": "Buffer"}
const bufferString = buffer.toString();
bufferString: Obst;Person;
after returning the bufferString to my main method: Set {"O", "", "b", "s", "t", ";", "P", "e", "r", "o", "n"}
I also tried to use .split()
const words = bufferString.split(";"); => ["Obst", "Person", ""] but when i return this array, then my map function displays only the letter O from the first keyword and no letters from the second keyword.
The strange thing is that when i create a new Array like let keywords = ["Obst", "Person", ""]; everything is working correctly.
So something must be odd with the strings coming from the buffer.
UCS-2 is a character encoding standard in which characters are represented by a fixed-length 16 bits (2 bytes)
With the encoding known you can decode properly and get the data as expected:
const data = [79, 0, 98, 0, 115, 0, 116, 0, 59, 0, 80, 0, 101, 0, 114, 0, 115, 0, 111, 0, 110, 0, 59, 0]
const dataBuffer = Buffer.from(data)
const utf16Decoder = new TextDecoder('UTF-16')
console.log(utf16Decoder.decode(dataBuffer)) // Obst;Person;
I think it's trying to decode as utf8 by default and getting upset at the unrenderable null bytes between every character:
I am trying to make a javaScript game of housie/bingo game. I used the library npm tambola-generator which returns auto generated tickets.
The server side process generates an array similar to this one ...
[{
"_entries": [[0,17,0,0,41,53,0,78,86], [4,0,0,35,0,58,67,80,0]/*, [ ... ] */],
}, {
"_entries": [[0,16,23,0,41,51,0,0,88], [2,20,0,31,43,56,0,0,0]/*, [ ... ] */],
}, {
"_entries": [[0,0,23,33,0,51,0,73,87], [1,0,0,35,42,58,68,0,0]/*, [ ... ] */],
}, {
// ... more items like that ...
}]
I want each ticket to feature a serial number which should be equal to a ticket items's array index but starting with the base of 1 instead of 0.
How does one include such a ticket number so that I can manage tickets for players?
I am just a beginner I couldn't solve it out. The target structure of the above example code should look like the following one ...
[{
"ticketNum": 1,
"_entries": [[0,17,0,0,41,53,0,78,86], [4,0,0,35,0,58,67,80,0]/*, [ ... ] */],
}, {
"ticketNum": 2,
"_entries": [[0,16,23,0,41,51,0,0,88], [2,20,0,31,43,56,0,0,0]/*, [ ... ] */],
}, {
"ticketNum": 3,
"_entries": [[0,0,23,33,0,51,0,73,87], [1,0,0,35,42,58,68,0,0]/*, [ ... ] */],
}, {
// ... more items like that ...
}]
Just use Array.prototype.map for achieving this task.
const entries = [{
"_entries": [
[0, 17, 0, 0, 41, 53, 0, 78, 86],
[4, 0, 0, 35, 0, 58, 67, 80, 0]
],
}, {
"_entries": [
[0, 16, 23, 0, 41, 51, 0, 0, 88],
[2, 20, 0, 31, 43, 56, 0, 0, 0]
],
}, {
"_entries": [
[0, 0, 23, 33, 0, 51, 0, 73, 87],
[1, 0, 0, 35, 42, 58, 68, 0, 0]
],
}];
const tcktEntries = entries.map((entry, index) => {
return {
ticketNum: index + 1,
...entry
};
});
console.log({
tcktEntries
});
.as-console-wrapper {
min-height: 100%!important;
top: 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So I'm trying to write a one-liner to sort an array where I just push all zeroes to the end of the array. This is my code:
const arr = [ 9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9 ];
arr.sort((a,b) => (a === 0 && b !== 0) ? 1 : (b === 0 && a !== 0) ? -1 : 0);
console.log(`[${arr.join(', ')}]`);
// [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
However, this code also seems to order other elements somehow, for the array
I expect
[9,9,1,2,1,1,3,1,9,9,0,0,0,0,0,0,0,0,0,0]
But I got
[9,9,9,9,1,2,1,1,3,1,0,0,0,0,0,0,0,0,0,0]
Any clues?
I am getting your expected result.
But you can also use filter since it returns a new array. Concat the first one (without zeroes) to the second one (only zeroes).
let arr = [ 9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9 ];
arr = arr.filter(a => a !== 0).concat(arr.filter(a => a === 0));
console.log(arr);
Pretty much a one liner... sort of. Uses a temp array to store the zeros
let n = [9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
let c = [], newn = n.filter(e => (e === 0 && c.push(e)) ? false : true).concat(c)
console.log(newn)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Edited to be more clear about my question:
I have a a multidimensional array that looks like this
var data = [
[2017, 1, 0, 0, 0, 0],
[2017, 0, 0, 23, 0, 0],
[2017, 0, 0, 0, 0, 9],
[2017, 0, 12, 0, 0, 0],
[2017, 0, 0, 0, 18, 0]
];
I'm trying to flatten this data into something that looks like this:
var data = [2017, 1, 12, 23, 18, 9];
Where in:
The year in Column #0 will be the same throughout rows.
For each row, Columns #1 through #5 will only have one non-zero element.
Is there an easy way to do this without having to process the data through multiple for loops? I was hoping maybe there was some native method in the Array type or function in a library out there.
Loop over the array and check take the number if it is higher than the current number:
var data = [
[2017, 1, 0, 0, 0, 0],
[2017, 0, 0, 23, 0, 0],
[2017, 0, 0, 0, 0, 9],
[2017, 0, 12, 0, 0, 0],
[2017, 0, 0, 0, 18, 0]
];
let newdata = [];
for (row of data) {
for (let i = 0; i<row.length; ++i) {
newdata[i] = (!newdata[i] || row[i] > newdata[i]) ? row[i] : newdata[i];
}
}
console.log(newdata);
outputs:
[2017, 1, 12, 23, 18, 9]
Use Array.map() in the 1st line to get a column index, and reduce all rows to a single non 0 value on that column:
const data = [
[2017, 1, 0, 0, 0, 0],
[2017, 0, 0, 23, 0, 0],
[2017, 0, 0, 0, 0, 9],
[2017, 0, 12, 0, 0, 0],
[2017, 0, 0, 0, 18, 0]
];
const result = data[0]
.map((_, i) =>
data.reduce((r, e) => r ? r : e[i], 0)
);
console.log(result);
Actually, based on the next two conditions:
The year in Column #0 will be the same throughout rows.
For each row, Columns #1 through #5 will only have one non-zero element.
You can use reduce() and findIndex() on the inners arrays to find the first number greater than zero whose index is not zero, and then put this on the related index of the accumulated result. This will improve perfomance a litle because you don't need to iterate the whole inner array on all the iterations of the reduce method.
var data = [
[2017, 1, 0, 0, 0, 0],
[2017, 0, 0, 23, 0, 0],
[2017, 0, 0, 0, 0, 9],
[2017, 0, 12, 0, 0, 0],
[2017, 0, 0, 0, 18, 0]
];
let res = data.slice(1).reduce((acc, curr) =>
{
let found = curr.findIndex((n, idx) => n > 0 && idx > 0);
acc[found] = curr[found];
return acc;
}, data[0]);
console.log(JSON.stringify(res));
I was just wondering for a while now what exactly "offsets" and "indices / index" are. Offsets are e.g. mentioned in https://github.com/mrdoob/three.js/blob/dev/src/core/BufferGeometry.js and indices were mentioned in IndexedGeometry, however I can't currently find it anymore in the dev tree.
Although indices seem rather obvious and although I could dive into the code to figure out some maybe-correct answer for myself I'd love to hear an "official" statement :)
Thanks!
There are two ways to defining geometries:
Non-Indexed
"vertices": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, ... ],
"normals": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, ... ]
In this mode every triangle position is as defined and you can't reuse data.
triangle 0: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
Indexed
"indices": [ 0, 1, 2, ... ],
"vertices": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, ... ],
"normals": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, ... ]
In this mode, indices define the order of the data. The first triangle is using indices 0, 1, and 2. These indices will be used to fetch the vertices and normals data:
triangle 0: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
The main benefit of indexed is the possibility of reusing data and uploading less data to the GPU:
"indices": [ 0, 0, 0, ... ],
"vertices": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, ... ]
triangle 0: [ 0, 1, 2, 0, 1, 2, 0, 1, 2 ]
As per offsets...
With offsets you can render specific ranges of your geometry. Instead of drawing from triangle 0 to triangle.length, you can draw from triangle 200 to triangle 400.