He has something to do with array-element-newline, But what I need is:
const a = [
// The first attribute must be below `[`
1,
2,
3,
// The last attribute must be above `]`
];
Related
I'm creating a document with pdfMake. In my document, I have three tables: the first show the three best equipments, the second shows the three worst equipments and the third shows all the equipments. It happens that the first table prints ok, but when I try to render the next tables, it does not work as expected (see this image).
The code is this:
const tableHeader = [
setTableHeader("Código"),
setTableHeader("OEE"),
setTableHeader("Disponibilidade"),
setTableHeader("Performance"),
setTableHeader("Qualidade"),
]
const equipmentsTable = [
tableHeader,
...sortedEquipsInfos.map((equip)=>[
equip.eECode,
equip.oee.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
equip.availability.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
equip.performance.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
equip.quality.toLocaleString('pt-BR', {maximumFractionDigits: 2}) + '%',
])];
...
const contentAux = [];
contentAux.push({
table: {
body: [
equipmentsTable[0], //header
equipmentsTable[equipmentsTable.length - 3],
equipmentsTable[equipmentsTable.length - 2],
equipmentsTable[equipmentsTable.length - 1],
]
}
});
contentAux.push({
table: {
body: [
equipmentsTable[0],
equipmentsTable[1],
equipmentsTable[2],
equipmentsTable[3],
]
}
})
contentAux.push(
{
table: {
body: equipmentsTable
},
}
)
...
var pdfDoc = printer.createPdfKitDocument({
...
content: contentAux
If I coment the first table, the second works fine. If I coment the first and the second, the third works. Any idea of what is happening?
This happens due to a known bug (or limitation) in pdfmake.
You cannot reuse the same object reference (equipmentsTable in your case) in the document definition. See https://github.com/bpampuch/pdfmake/issues/465
A possible workaround is to copy the objects (in all 3 cases):
...
const equipmentsTableCopy1 = JSON.parse(JSON.stringify(equipmentsTable));
...
[
equipmentsTableCopy1[0],
equipmentsTableCopy1[1],
equipmentsTableCopy1[2],
equipmentsTableCopy1[3],
]
...
Just took up TypeScript a couple of weeks ago without much knowledge in JavaScript.
I am trying to go through all the files in the specified directory and put each file name (string) and change time (number) into an array of array and sort by change time.
It looks like this: [['natalie.jpg', 143], ['mike.jpg', 20], ['john.jpg', 176], ['Jackie.jpg', 6]]
Problem 1: I do not know how to specify the inner array content, string and number. Type? Interface? Class? Tuple?
Problem 2: I do not know how to sort by change time in ascending order, so that the array changes to: [['Jackie.jpg', 6], ['mike.jpg', 20], ['natalie.jpg', 143], ['john.jpg', 176]]
import fs from 'fs'
const dirPath = '/home/me/Desktop/'
type imageFileDesc = [string, number] // tuple
const imageFileArray = [imageFileDesc] // ERROR HERE!
function readImageFiles (dirPath: string) {
try {
const dirObjectNames = fs.readdirSync(dirPath)
for (const dirObjectName of dirObjectNames) {
const dirObject = fs.lstatSync(dirPath + '/' + dirObjectName)
if (dirObject.isFile()) {
imageFileArray.push([dirObjectName, dirObject.ctimeMs]) // ERROR HERE!
}
}
imageFileArray.sort(function (a: number, b: number) {
return b[1] - a[1] // ERROR HERE! Can we do something like b.ctime - a.ctime?
})
} catch (error) {
console.error('Error in reading ' + dirPath)
}
}
readImageFiles(dirPath)
console.log(imageFileArray)
import * as fs from 'fs'
// Read files in folder
const files = fs.readdirSync( './files' )
// This will store the file name and their modification time
const imageFileArray: [ string, Date ][] = []
for ( const file of files ) {
// You can get a file's last modified time through fs.stat
const stats = fs.statSync( `./files/${file}` )
const modifiedTime = stats.mtime
// Push a tuple containing the filename and the last modified time
imageFileArray.push( [ file, modifiedTime ] )
}
// Sort from older to newer
const sorted = imageFileArray.sort( (a, b) => a[1].getTime() - b[1].getTime() )
console.log( sorted )
The modified time returns a Date object. If you want to sort from newer to older, just invert the operation in the sort function.
If I have an object like this how can I sort using the underscore module in node.js so the values are highest to lowest so that...
{ ZZX: 1, FRA: 5, GBR: 2, USA: 3 }
..becomes
{ FRA: 5, USA: 3, GBR: 2, ZZX: 1 }
You can do
sortedKeys = _.sortBy(Object.keys(obj), function(key){ return obj[key]; }).reverse();
newHash = {};
sortedKeys.forEach (e => (newHash[e] = obj[e]));
console.log(newHash)
For instance, I send a request like { "arr": [ 1, 2, 3, 4 ] } in front-end.Then I get a request.body in Node.js.But the request.body shows as { "arr[]": [ 1, 2, 3, 4 ] }.I can't find out what the wrong it is.I just receive a wrong keyname in my body's ojbect.How to deal with it?
You are using jQuery.ajax with traditional set to false. It is not "wrong"; some frameworks or languages (notably PHP) expect it that way. If you do not, change the parameter to true.
I have the following code, can anyone tell the difference:
const _ = require('lodash');
const arr = [
{'fname':'Ali', 'lname': 'Yousuf'},
{'fname': 'Uzair', 'lname': 'Ali'},
{'fname': 'Umair', 'lname': 'Khan'}
];
_.map(arr, 'fname');
_.pluck(arr, 'fname');
The output is the same, and both functions are not mutating arr.
In the way you're using them, they basically do the same. That's why .pluck() was removed from Lodash v4.0.0 in favor of using .map() with a string as second argument.
Here's the relevant excerpt from the changelog:
Removed _.pluck in favor of _.map with iteratee shorthand
var objects = [{ 'a': 1 }, { 'a': 2 }];
// in 3.10.1
_.pluck(objects, 'a'); // → [1, 2]
_.map(objects, 'a'); // → [1, 2]
// in 4.0.0
_.map(objects, 'a'); // → [1, 2]