[Solved] edit JavaScript files using node js

I was able to edit a JavaScript file using regex. my code is below: function updateFile(filename, replacements) { return new Promise(function(resolve) { fs.readFile(filename, ‘utf-8’, function(err, data) { var regex, replaceStr; if (err) { throw (err); } else { regex = new RegExp(“(\\” + ‘let’ + “\\s* ]*” + replacements[0].rule + “\\s*=\\s*)([^\\n;}]+)([\\s*;}])”); replaceStr = “$1” + … Read more

[Solved] how to replace the value by comparing two json and to update in the created table

First, you will have to write a function that will iterate the subjectDetails array and find the subject name based on a label. let subjectDetails = [{ “subjectLabels”: { “eng”: “english”, “sci”: “environment science”, “soc”: “History & Geo” } }] const getSubjectName = (label) => { let name = label; subjectDetails.forEach(details => { if(details.subjectLabels.hasOwnProperty(label)){ name … Read more

[Solved] Reseting a const variable, even though it’s const (ES6) [duplicate]

What you’re doing is called variable shadowing. When you declare a variable with const (or let) it’s block-scoped: the second time you’re declaring a new response constant you’re in a different scope, so they’re different variables. But the fact that they shares the same name, means you’re shadowing the outer one (that could potentially lead … Read more

[Solved] How can I write a function that will format a camel cased string to have spaces?

You can use regex to split on capitals and then rejoin with space: .split(/(?=[A-Z])/).join(‘ ‘) let myStrings = [‘myString’,’myTestString’]; function myFormat(string){ return string.split(/(?=[A-Z])/).join(‘ ‘); } console.log(myFormat(myStrings[0])); console.log(myFormat(myStrings[1])); solved How can I write a function that will format a camel cased string to have spaces?

[Solved] Merge objects in an array if they have the same date

This is a good use case for reduce. const rawData = [ { date: ‘3/10/2019’, a: ‘123’, }, { date: ‘3/10/2019’, b: ‘456’, }, { date: ‘3/11/2019’, a: ‘789’, }, { date: ‘3/11/2019’, b: ‘012’, }, { date: ‘3/11/2019’, c: ‘345’, } ]; const groupByDate = array => array.reduce((results, item) => { const current = … Read more

[Solved] JavaScript reduce with ternary operator

Essentially, the code you provided is looping through each element in votes and checking whether it is greater than an element stored at a particular index. This index is stored in the variable bestIndex an is used to mark/keep track of the index which holds the largest element from all elements seen while looping. In … Read more

[Solved] how to check if an object has at least one true value [duplicate]

Assuming that values is actually an object, check if .some of the Object.values of the object are true: const values = {de: true, en: false, nl: false, pl: false, ru: false}; const someTruthy = Object.values(values).some(val => val === true); console.log(someTruthy); (if the only truthy value is true, you can use (val => val) instead) solved … Read more

[Solved] Syntactic sugar JavaScript ( If statement) Error [closed]

I try to use syntactic sugar The conditional operator is not syntactic sugar. It’s a specific operator with a specific purpose, and you’re simply using it incorrectly. It is used for conditionally producing a value as an overall expression, one value if the condition is true and another if it’s false. For example: let x … Read more

[Solved] How to concatenate this array [duplicate]

const bidder= [ [1,2,3,4], [5,6,7,8] ] const arr1 = [9,10,11,12]; const result = […bidder, arr1]; console.log(result); You can use spread operator to spread whatever is in bidder array and add your other array as the last one in a new array. solved How to concatenate this array [duplicate]

[Solved] replace array of object with another array of object base on property

You can do it using Array#map() to create a new array and Array#find() to find the object in the second array let arr=[{status:”ok”},{status:”ok”},{status:”error”}], arr2=[{status:”error”,msg:”etc”,”more property”:!0}]; arr = arr.map(a=>{ let fullObj = arr2.find(a2=>a2.status===a.status); return fullObj ? fullObj : a; }); console.log(arr); 3 solved replace array of object with another array of object base on property