I am trying to make it small (short), but it is not happening, please tell me where I am wrong. And where I am wrong, you correct there...
var set1 = document.querySelector(".get1");
var set2 = document.querySelector(".get2");
var set3 = document.querySelector(".get3");
var set4 = document.querySelector(".get4");
to..
var ("[set1],[set2],[set3],[set4]")
= document.querySelector("[.get1],[.get2],[.get3],[.get4]")
but it's not work
First code is work very well but the second code which is a shortened form of the first code is not working
You will have to use .map() and destrcutring to transform your given array and then assign it to individual values. You can do this:
let [var1,var2,var3,var4] =
['.get1','.get2','.get3','.get4'].map(x => document.querySelector(x));
But as mentioned your code is fine the way it is. It is simpler and easier to understand.
Related
I'm trying to use set.has() in lightning web components and it seems to be not working.
Below is the code snippet..
sStatusToVerify = 'Complete';
var setStatusVals = [...new Set(this.lstAllData.map(obj => obj.sStatus))];
console.log('setStatusVals : ',setStatusVals);
console.log('Contains?? : ' ,setStatusVals.has(sStatusToVerify));
setStatusVals consoles all the values and it contains "Complete". However, the next console is not printed at all. It should ideally print true. Not sure why this is not working.
What is wrong here?
The problem with your solution is , you are converting the set back to array by using the spread operator [... new Set()] and array does not has has method . Hence the issue
var sStatusToVerify = 'Complete';
var arr=[{sStatus:'Complete'},{sStatus:'Start'},{sStatus:'InProgress'}];
var setStatusVals = new Set(arr.map(obj => obj.sStatus));
console.log(setStatusVals.has(sStatusToVerify));
I want to generate some dates between two fixed values, but I don't know how to
use faker.date.between to achieve that.
the example in faker js demo is just giving a null value.
Not sure when did they update wiki, but they do have a working sample for date.between now
faker.date.between('2015-01-01', '2015-01-05');
Their code from Github shows:
self.between = function (from, to) {
var fromMilli = Date.parse(from);
var dateOffset = faker.random.number(Date.parse(to) - fromMilli);
var newDate = new Date(fromMilli + dateOffset);
return newDate;
};
And I don't know how you're using it, or why it's not working on their example.... But this should give you some direction, at least.
If this does not help you, or is still producing an undesired result, I would open an issue up on their github.
I was trying to crawl a very old website for a specific tag, I need to get it by it's for= attribute. So I used this piece of code.
var character = document.querySelectorAll("label[for=char_1]");
For some reason it returns an undefined, but I was using this script for a few days now and it worked like a charm. Here's the fun part. Typing that command in browsers console will result in undefined. But typing this alone:
document.querySelectorAll("label[for=char_1]");
Will return a proper NodeList. Why it won't assign to a variable...?
edit: It seems that deleting var and typing character without it will make it work. It's resolved but I would still love to get an answer to "why is this happening"?
edit2:
for (var i = 0; i < 15; i++) {
var character = document.querySelectorAll("label[for=char_" + i +"]");
console.log(character); // this will return [] from the script.
var color = character[0].children[0].style.color;
}
A simple for loop. All I get is Cannot read property 'children' of undefined. But I can type in the very same command document.querySelectorAll... and it will work in the browser and will return NodeList.
I had it working like this in a very hacky script. It worked.
var character1 = document.querySelectorAll("label[for=char_1]");
var characterColor1 = character1[0].children[0].style.color;
edit3:
var character1 = document.querySelectorAll("label[for=char_1]");
var characterColor1 = character1[0].children[0].style.color;
var character2 = document.querySelectorAll("label[for=char_2]");
var characterColor2 = character2[0].children[0].style.color;
// ...
The above code works without a single problem though. I don't think it's DOM not being ready as this code is also run from Greasemonkey script and it works. The only difference is the for loop.
var x = ""; // returns undefined, because it's a var assignment.
var elements = document.querySelectorAll('div');
That's expected behavior when pasted into the console.
edit: It seems that deleting var and typing character without it will make it work. It's resolved
I'm afraid you're creating a global scope variable now. or perhaps characters is an already defined variable in that scope.
Buhah, as I said in edit 3 "the only difference is the for loop". I was so busy trying to find an answer in the DOM-related things that I made the simplest mistake ever done in programming.
See?
char_1
With...
for(var i = 0...)
0! And I was testing char_1 in the browser instead of char_0. Which - truly - returns [] instead of something useful. Time to go on a holiday break I guess, my brain seems to be there already. :)
In ExtJS, using an Ext.Array (after using Ext.Array.difference), I get a resulting array and would like to know the best way to check if the array is empty?
I did use theArray.length as one could do in javascript, but I'm wondering if there is a better way/faster to acheive that? (At first I thought that isEmpty would help but it seems to be working on object, not array)
You can easily add this to the Array prototype like this:
Array.prototype.isEmpty = function(){
return !this.length;
};
var a = ['a','b','c','d'];
var b = ['b','d','f','h'];
var c = Ext.Array.difference(a,b);
var d = [];
console.log(c.isEmpty(), d.isEmpty());
Hope it helps :)
I was recently handed a partially-completed project and I'm new to using Bottle. I have a situation where I'm sending a dictionary inside an AJAX request and Bottle is on the server side. The dictionary looks something like this (in JavaScript):
var myInt = 5;
var myArray = [0, 1, 2];
var data = {
myInt: myInt,
myArray: myArray
};
Then, in Python, I want to read in this data. I can't seem to get the array. I can get the int just fine. I've tried several things, but here's what made most sense to me that still didn't work:
try:
myInt = int(request.forms.get('myInt'))
myArray = request.forms.getall('myArray')
This results in an empty list for myArray. Can anyone point me in the right direction? I've read every similar question on StackOverflow to no avail.
I know I could turn this into a string separated by pipes or something, but I would like to see if arrays are possible to send like this.
Thanks!
Use request.json:
try:
myInt = request.json['myInt']
myArray = request.json['myArray']
(Assumes your client is setting content-type to application/json.)
Here's a nice example.
Not sure whether this is the only problem, but (I think) you may be confusing JSON arrays and the use of getall. You should use get, not getall, here:
myArray = request.params.get('myArray')
If I understand the code correctly, getall is for the case when you have multiple args with the same name, as in http://example.com/get?foo=1&foo=2.
On the other hand, your myArray variable is just a single reference to an array.
Hope this helps (even if it doesn't solve your problem).
var myInt = 5;
var myArray = [0, 1, 2];
var data = {
myInt: myInt,
myArray: JSON.stringify(myArray)
};
Use JSON.stringify in js and in bottle json_loads():
myInt = request.params.get('myInt')
myArray = json_loads(request.params.get('myArray'))