Better way to fill variables from array (Destructuring assignment) - javascript

I have this Code in my Javascript File:
const blfTypeAndId = DDid.split("PrerequisitesSelect");
const blfType = blfTypeAndId[0];
const blfId = blfTypeAndId[1];
The blfType is after this either 'block', 'line' or 'field' and the blfId is _B1_L1_F1 while the numbers can be different (this is just for field, for line it would be _B1_L1 and block just _B1)
In a world where Python and Javascript are combined I would just do this:
const blfType, const blfId = DDid.split("PrerequisitesSelect");
Is there a nice way in Javascript to still do this in one line or is my first written code already the best possible solution?

A good job for the Destructuring Assignment
const blfTypeAndId = DDid.split("PrerequisitesSelect");
const [blfType, blfId] = blfTypeAndId;
or even just
const [blfType, blfId] = DDid.split("PrerequisitesSelect");

Related

Compare two arrays with node.js and puppeteer

I build on a web-scrapper, that, lets say scrap URLs from google
I get an array of URLs from google results:
const linkSelector = 'div.yuRUbf > a'
let links = await page.$$eval(linkSelector, link => {
return link.map( x => x.href)
})
the output of 'links' is something like that:
[
'https://google.com/.../antyhing'
'https://amazon.com/.../antyhing'
'https://twitter.com/.../antyhing'
]
Now I have a 'blacklist', with something like that:
[
'https://amazon.com'
]
At the moment I stuck at that point where I can compare both arrays, and remove these URLs from 'links' which are listed within my blacklist.
So I came up with the idea, to get the domain of the url within my links array - like so:
const linkList = []
for ( const link of links ) {
const url = new URL(link)
const domain = url.origin
linkList.push(domain)
}
Yes, now i got two arrays which i can compare against each other and remove the blacklisted domain, but i lost the complete url i need to work with...
for( let i = linkList.length - 1; i >= 0; i--){
for( let j=0; j < blacklist.length; j++){
if( linkList[i] === blacklist[j]){
linkList.splice(i, 1);
}
}
}
Code Snippet is part of the give answer, here:
Compare two Javascript Arrays and remove Duplicates
Any ideas how can i do this, with puppeteer and node.js?
I couldn't find an obvious dupe, so converting my comments to an answer:
.includes:
const allowedLinks = links.filter(link => !blacklist.some(e => link.includes(e)))
.startsWith:
const allowedLinks = links.filter(link => !blacklist.some(e => link.startsWith(e)))
The second version is more precise. If you want to use the URL version, this should work:
const links = [
"https://google.com/.../antyhing",
"https://amazon.com/.../antyhing",
"https://twitter.com/.../antyhing",
];
const blacklist = ["https://amazon.com"];
const allowedLinks = links.filter(link =>
!blacklist.some(black =>
black.startsWith(new URL(link).origin) // or use ===
)
);
console.log(allowedLinks);
As for Puppeteer, I doubt it matters whether you do this Node-side or browser-side, unless these arrays are enormous. On that train of thought, technically we have a quadratic algorithm here but I wouldn't worry about it unless you have many hundreds of thousands of elements and are noticing slowness. In that case, you can put the blacklisted origins into a Set data and look up each link's origin in that. The problem with this is it's a precise ===, so you'd have to build a prefix set if you need to preserve .startsWith semantics. This is likely unnecessary and out of scope for this answer, but worth mentioning briefly.

Possible to use R.__ with semver functions?

const semvers = ["5.100.0-rc.0", "5.97.3", "5.97.1"];
const newRecord = "5.97.2";
Given the above test data, I wish to insert newRecord into the right order, defined/sorted by semver package.
result = ["5.100.0-rc.0", "5.97.3", "5.97.2", "5.97.1"];
Below is my attempt which gave me the correct result
const semvers = ["5.100.0-rc.0", "5.97.3", "5.97.1"];
const newRecord = "5.97.2";
const indexResult = R.findIndex(x => semver.lt(x, newRecord))(semvers);
const result = R.insert(indexResult, newRecord, semvers)
Then, i was wondering if I can replace x with R.__, so i attempted below
const indexResult = R.findIndex(semver.lt(R.__, newRecord))(semvers);
I had the impression that R.__ referring to the arguments that was gonna passed but seems like it's not, or it was simply due to the fact that semver.lt is not a curried function and hence couldn't comprehend R.__?
R.__ works with Ramda functions or functions curried with Ramda e.g.,
const semvers = ["5.100.0-rc.0", "5.97.3", "5.97.1"];
const newRecord = "5.97.2";
const findVer = R.curryN(2, semver.lt)(R.__, newRecord);
const indexResult = R.findIndex(findVer, semvers);
const result = R.insert(indexResult, newRecord, semvers);
My preferred option would have been: R.flip(semver.lt)(newRecord) unfortunately semver.lt arity is 3 (third argument is a loose parameter) so R.flip doesn't work straight out of the box.
With R.partialRight you could supply the last two arguments (including that undocumented (?) loose parameter):
const findVer = R.partialRight(semver.lt, [newRecord, false]);
But honestly what you had originally is fine.

How to use reduce and the ramda "hex2color" function to count list of hex values than have r in their color name?

"Use reduce and the hex2color function to count list of hex values than have r in their name";
My current attempt is below. The first piece I know needs to be fixed is the filter function. I need to be able to filter out any colors that have the letter "r", but cannot seem to find a way to easily fit that into the filter function. It could easily be a syntax issue as I think I am asking the filter to find any strings that === "r", even though I am trying to use "contains" to solve that and have it check the whole color word.
Once the filter function is working, I assume the next step is to simply use the reduce function, then compose them together. ( I could be way off off, however).
I am quite new to programming, any insight is extremely welcome. Thanks!!
const exercise3 = _ => {
const hexes = ["#0000ff", "#f5f5dc", "#cd853f", "#663399", "#ffa500"];
const letter = "r";
const mapper = hex2color;
console.log(map(mapper, hexes)); //blue,beige,peru,rebeccapurple,orange
const filterer = el => contains(hex2color(el), letter);
console.log(filter(filterer, hexes)); //yields nothing, I assume to using the filter wrong with "r".
const reducer = (acc, el) => acc + 1;
const mappedFn = map(mapper);
const filtererFn = filter(filterer);
const reducerFn = reduce(reducer, 0);
const composedFn = compose(reducerFn, filtererFn, mappedFn);
return composedFn(hexes);
};

Regex using array.filter in javascript

I'm trying to use a regular expression to filter the name column in a csv file that has be put into an object array. Everything works fine if I type the exact value.
values I want "king" to match are below:
kingkong, king kong, king-kong, king_kong, king11, kongking, kong-king, kong_king, kong king, 11king
I've tried using filter and find methods but I want to use filter to return multiple values if they exist. I have tried the following regex but can't figure out the proper sytax if it is even correct.
const CSVToJSON = require('csvtojson');
const user = "king";
CSVToJSON().fromFile("./locations.csv").then(source => {
var found = source.filter(function(v, i){
return ((v["name"]== /\bking.*/g));
})
You can use the following approach.
const CSVToJSON = require('csvtojson');
CSVToJSON().fromFile("./locations.csv").then(source => {
var found = source.filter(function(v, i){
return ((v["name"].match(/king/g)));
});
return statement could be something like
return ((/king/g).test(v["name"]));
OR
return ((v["name"].match(/king/g)));
Both should work
However, your sample patterns show that king might stand either at the beginning or at the end of the target (bot can't have both prefix and suffix). If I am right, that means you don't need regex for that.
const CSVToJSON = require('csvtojson');
const user = "king";
CSVToJSON().fromFile("./locations.csv").then(source => {
var found = source.filter((v, i) => v.startsWith(user) || v.endsWith(user))
/*rest of the code */
});
If king can stand anywhere, you can simply use includes instead.
This is what worked, I'm totally new to JavaScript:
const user = args;
var regex = new RegExp(user, "g");
CSVToJSON().fromFile("./locations.csv").then(source => {
var found = source.filter(function(v, i){
return ((v["name"].match(regex)));
})

How to select a variable based on string value

For example:
labelsSetOne = [1,1,1,1,1]
labelsSetTwo = [2,2,2,2,2]
const printLabels = set => {
const set = `labelsSet${set}`
console.log(set)
}
printLabels('one')
One solution is using eval: eval('labelsSet${set}') however, we all know eval is not save or accepted by a lot of people, another way is window['labelsSet${set}'] however, in my case it is not a global variable.

Categories

Resources