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:
Related
Greeting guys , please take a look at this code
var array = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139];
function addHundredIfDivisionBY3Possible (element , i){
if (element % 3 === 0) {
array[i]= (element +=100) ;
}
}
array.forEach(addHundredIfDivisionBY3Possible);
console.log (array);
output:
[ 112, 929, 11, 103, 199, 1000, 7, 1, 124, 37, 4, 19, 400, 3775, 299, 136, 209, 148, 169, 299, 106, 109, 20, 58, 139, 59, 103, 1, 139 ]
the question is how did the code understand that the "element" parameter is the value and i is for index , supposedly i meant it the other way around and i wrote the function like this
// wanting the parameter 'element' to be the index and the parameter i to be the value of the element
function addHundredIfDivisionBY3Possible (element , i){
if (i % 3 === 0) {
array[element]= (i +=100) ;
}
}
why is the first one working and the second one is not , it may have to do to with me trying to index the element un-properly " array[element] " but the question remains .. how does the program understand which parameter mean what ?
tried looking it up on google
// this is how forEach works (kind of)
function forEach(array, callback) {
for (let i = 0; i < array.length; i += 1) {
// element is passed first
// index is passed second
callback(array[i], i)
}
}
forEach(['a', 'b', 'c', 'd'], (e, i) => console.log('element', e, 'index', i))
How do I convert a Nest request object with a buffer into JSON?
Everything I've logged either shows an array of bytes or a string.
Here is my controller:
export class KycresultsController {
#Post()
create(#Body() data: any): string {
const j = data.toJSON();
var buf = Buffer.from(JSON.stringify(data));
var temp = JSON.parse(buf.toString());
var f = temp.data;
console.log(f);
const decodedJsonObject = Buffer.from(f, 'base64').toString('ascii');
console.log(decodedJsonObject);`
And I send this POST:
curl -X POST localhost:3001/kycresults -d "emsample=1&data=2"
I see in my logs:
[
101, 109, 115, 97, 109, 112,
108, 101, 61, 49, 38, 100,
97, 116, 97, 61, 50
]
emsample=1&data=2
[
101, 109, 115, 97, 109, 112,
108, 101, 61, 49, 38, 100,
97, 116, 97, 61, 50
]
emsample=1&data=2
I want to get {emsample: 1, data: 2}
I have checked Check if an array is subset of another array and related questions but they aren't quite the same. They wanted to see if all elements of a sub array are within some other array (independent of order), which you can tell by the answers is fairly straightforward. I am not sure if now the following question is as straightforward.
I am trying to write JavaScript compiler testing for a compiler written in JS (it doesn't compile JS, in my situation now it just has to do with testing the final memory layout is what is expected). So I am trying to write a Jest matcher. The matcher part is easy but the algorithm for matching the grid of elements doesn't seem easy and is making my head spin.
Basically, this is sort of like an image matching problem. How do you find if a square/rectangle of pixels is exactly within another image? And do so somewhat efficiently?
In the same way, I would like to find is a sequence of bytes in a "test" array is within a larger array, in the exact order as defined.
So for example:
var given = [
1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
21, 23, 25, 27, 29, 31, 33, 35, 37,
39, 41, 43, 45, 47, ...
]
var expectedSubset = [
13, 15, 17, 19, 21, 23, 25, 27
]
expect(given).toContainSequence(expectedSubset)
Any ideas how to do this? Maybe it's simple, but it seems complex.
Where it seems to get tricky is here:
var given = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 5, 8, 0, 0, 0, 0, 0, 0,
0, 5, 8, 11, 14, 0, 0, 0, 0, 0, 0,
...
]
var expected = [ 0, 0, 0, 0, 5, 8, 11 ]
I have an array
const myArr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ]
I need to split into digits like this:
const splited = [ 9, 4, 9, 5, 9, 6, 9, 7, 9, 8, 9, 9, 1, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 5, 1, 0, 6 ]
You could join the items, split and map numbers.
var array = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106],
pieces = array.join('').split('').map(Number);
console.log(pieces);
Same approach, different tools.
var array = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106],
pieces = Array.from(array.join(''), Number);
console.log(pieces);
map each number to a string and split the string, and spread the result into [].concat to flatten:
const myArr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ];
const splitted = [].concat(...myArr.map(num => String(num).split('')));
console.log(splitted);
You can use reduce function to create a new array and use split to split the number converted to string
const myArr = [94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106]
let newArr = myArr.reduce(function(acc, curr) {
let tempArray = curr.toString().split('').map((item) => {
return +item;
});
acc.push(...tempArray)
return acc;
}, [])
console.log(newArr)
My input in one single json
input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
In the output i want to iterate through all the elements of this json and sum the values of the matching keys. Also keep the non matching lone keys in the output as well.
output =
[{
"201510": 5, // no matching pair
"201609": 3, // no matching pair
"201610": 6+9 = 15, // matching pair exist
"201611": 10+12 = 22,
"201803": 20,
"201804": 30+13 = 33,
"201805": 40+14 = 44,
"201806": 130,
"201809": 130,
"fy16Q3": 17, // no matching pair
"fy17Q1": 2+7 = 9, // matching pair exist
"fy17": 3+8 = 11,
"fy17Q2": 5+9 = 14,
"fy17Q3": 6+100 = 106
}];
The problem is that iam not able to figure out how to handle the keys which don't have a matching pair.
You can try the following code. Your desired output looks different than your logic
var data = input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
var output = data.reduce((arr,d,x) =>{
var keys = Object.keys(d);
keys.forEach( (k) => {
if(!arr[k]) arr[k] = 0;
arr[k] = arr[k] + d[k];
})
return arr;
},{});
console.log(output);
input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
var output = input.reduce((p,c) => {
for(let k in c){
p[k] = (p[k] || 0) + c[k];
}
return p;
},{});
console.log(output);
input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
var output = [{}];
for( i in input){
for (key in input[i]){
if(output[0].hasOwnProperty(key)){
output[0][key]+=input[i][key];
}else{
output[0][key]=input[i][key];
}
}
}
console.log(output)
Use array reduce method. In this method take the first object of the input array as the initial object since.SInce object keys are always unique, for any matching key just update the value
var input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 3
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
// the array will start reducing from second element that is
// element from index 1
let toLoopArray = input.slice(1, input.length);
let output = input.reduce(function(acc, curr) {
// for the current object check if the key already exist
// if not then create the new key and update value
for (let keys in curr) {
if (!acc.hasOwnProperty(keys)) {
acc[keys] = curr[keys]
} else {
// acc[keys] = acc[keys] + curr[keys]
console.log(curr[keys])
acc[keys] = acc[keys] + curr[keys]
}
}
return acc;
}, input[0])
console.log([output])