I am not sure i named the title properly. Let me explain my little problem.
My code snipppet is pretty short:
var myArray= new Array([new Date()],[123],[abc]);
var time = myArray[0].getTime()
I want the time saved in the object inside the array to be written to the "time" variable.
I just thought this would work but i get an error.
Uncaught TypeError: myArray[0].getTime is not a function
Apparently i am doing it wrong, but i have no idea what is the problem and how to do it right.
Its inside of another array:
var time = myArray[0][0].getTime();
// ^^^
Working example:
var myArray = new Array([new Date()], [123], ['abc']),
time = myArray[0][0].getTime();
alert(time);
You define array of arrays [[], [], []], so to get date element you should do this:
var myArray= new Array([new Date()],[123],['abc']);
var time = myArray[0][0].getTime();
The code you used creates an array with arrays inside:
var myArray= new Array([new Date()],[123],[abc]); // [[new Date()],[123],[abc]]
So you would have to call
myArray[0][0].getTime()
You probably wanted to do
var myArray = new Array(new Date(),123,abc); // [new Date(),123,abc]
Or simply
var myArray = [new Date(),123,abc];
As a tip, when you get these errors, try console.loging the variables. You would see the date you expected was actually an array with a single position, and would easily found out the error :)
Related
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.
I have some requirement like I need to get the values from a map actually where the format is as below:
"{xyz=True, abc=asd-1123, uvw=null}"
to get the values from this map which is in string.
I tried to use JSON.parse("{xyz=True, abc=asd-1123, uvw=null}") and also tried using var map = new Map(JSON.parse("{xyz=True, abc=asd-1123, uvw=null}"))
But neither way it was not working
well to start, json.parse is not going to work as that is not valid json. you are going to need to do the parsing yourself. So assuming everything is consistently in that method, you should start by stripping off the curly braces then breaking down your values and creating your map from those values.
So as an example:
let map = new Map(); // or create an object: {}, because you can get values using Object.values(yourObject)
let mapStr = "{xyz=True, abc=asd-1123, uvw=null}".slice(1, -1);
let kvp = mapStr.split(', ');
for(let i = 0; i < kvp.length; i++) {
let splitVal = kvp[i].split(=);
map.set(splitVal[0], splitVal[1]);
}
Then you can just use the map object you created.
Take into consideration this assumes none of your key-value-pairs have an "=" sign in them. you can also further investigate regexs to generate your maps. either way, it's on you to do the mapping unless there is a library that does this for you already in existence :)
Here's the code.
var postData={};
event.stopPropagation();
postData.action='preview';
postData.data=$("form#gaarrl").serializeArray();
var n=[];
n['name']='media';
n['value']=imgName;
postData.data.push(n);
console.dir(postData);
$.post("database.php",{postData },
The console.dir command shows the media:imgName as a part of the postData.data as expected but the database.php $_REQUEST only shows the elements from the serializeArray step.
What is happening?
Thanks,
Jim.
Try changing var n = []; to var n = {};.
This fixed it for me.
This is because normal Javascript arrays do not allow keys, just numerical indexes. {} is shorthand for new Object(), and allows you to give it multiple named attributes.
I really love Javascript and I wrote my code like this. I feel like it should work. Am I doing it in the wrong order? If it won't work like this why not?
var mydate = new Date();
alert( mydate.toLocaleTimeString().split(":").pop().join(':'));
split() makes it an array, pop() takes off the end of the array, join() makes it a string again right?
You could use Array#slice with a negative end/second argument.
Array#pop returns the last element, but not the array itself. slice returns a copy of the array with all emements from start without the last element.
var mydate = new Date();
console.log(mydate.toLocaleTimeString().split(":").slice(0, -1).join(':'));
No, pop() will remove the last element from the array and return it.
To achieve what you're trying, you'll need to first assign the result of split() to a variable you can then reference:
var mydate = new Date(),
myarr = mydate.toLocaleTimeString().split(':');
myarr.pop();
console.log(myarr.join(':'));
if all you want to achieve is hours:minutes, you can just simply do this
var mydate = new Date();
console.log(mydate.getHours() + ':' + mydate.getMinutes());
You are trying to use method chaining where next method in the chain uses the output of the previously executed method. Reason it's not working is because "join()" method is a prototype of an array but "pop()" returns an array element which doesn't aforementioned method that's why the error.
refactor your code as below:
var myDate = new Date(),
myDateArr = myDate.toLocaleTimeString().split(':');
myDateArr.pop(); // Remove the seconds
myDate = myDateArr.join(':'); // Returns string
console.log(myDate);
Hope this helps.
Try this
var mydate = new Date();
alert( mydate.toLocaleTimeString().split(":").slice(0, 2).join(":"));
Fairly new to programming. I've set up a prompt command to have the user input text or a number which is stored to an array, but the while loop seems to be rewriting over the array value each time it loops.
The array is acting like a variable only storing one value
var course = new Array();
var grade = new Array();
while(confirm("Would you like to add a course?"))
{course = prompt("Enter the course code. Example - ABC1234");
To add an element to an array, use array.push.
Change the last line to look like this:
course.push( prompt("Enter the course code. Example - ABC1234") );
You also have a missing } at the end. So your entire code snippet looks like this:
var course = new Array();
var grade = new Array();
while(confirm("Would you like to add a course?")){
course.push( prompt("Enter the course code. Example - ABC1234") );
};