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.
Related
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.
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");
Hi I want to create a global variable in react but I would like to avoid it, so I created a component and want to store string data in it such as below:
export default function settings() {
const [stringData, setStringData] = useState("");
I want this state to concactnate input string. For example
setStringData("I am ");
setStringData(" a ");
setStringData("robot.");
When I retrieve stringData I want it to be printed as
I am
a
robot.
How do I do this ?
When you calling setStringData it is the same as though you just assign a value to a variable, e.g. stingData = 'asdf' so if you need to concatenate strings you need to add the current value and the string you want to add, below you can find an example.
const [stringData, setStringData] = useState("");
setStringData(stringData + "Some Sting To Append!");
UPD: Based on the comments, showing an example with some listener.
const [stringData, setStringData] = useState("");
const addString = () => setStringData(stringData + " Clicked!");
return (<button onClick={addString}> Add Text </button>);
"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);
};
Is it possible to enter only one argument to SetState that contains both the name and the value. See the example below. Is there something wrong with the brackets?
This would be handy when changing a lot of states at the same time. That is, first prepare them in one long string and execute setState only once. Thank you!
this.setState({myState: "help"}) // this works of course
whatstate='myState'
this.setState({[whatstate]: "me"}) // this too
whatstate2='myState: "please"'
this.setState(whatstate2) // but how to make this work?
// if you like to work only with strings
var whatstate = {};
whatstate['myState1'] = 'help';
whatstate['myState2'] = 'me';
whatstate['myState3'] = 'please';
// ^ this will produce an object equivalent to this
//whatstate = {
// myState1: 'help',
// myState2: 'me',
// myState3: 'please'
//}
// which you can use it to 'setState'
this.setState(whatstate);
You can call this.setState({ whatstate2 }) to achieve the same effect. This is the property value shorthand from ES6.
Reference: https://ariya.io/2013/02/es6-and-object-literal-property-value-shorthand
In case you'd like to update multiple states in one go, you can also do that like this.
this.setState({
myState1 : newState1,
myState2 : newState2
});
If the variable names are the same as the state names as mentioned previously, you can do.
this.setState({ myState1, myState2 });