Via ajax-based script i get object:
for example:
item.TYP_PCON_START
which value is, for example 201212...
When i try to slice him, i get oject error...
How could i slice this object so, that for example i get 2012, or better set two last numbers on furst place and add dot, like:
12.2012
How could i do this? (i append this text as value of select list)
You need to slice the string property, not the object itself:
item.TYP_PCON_START.slice(-2) + '.' + item.TYP_PCON_START.slice(0, 4);
> '12.2012'
http://jsfiddle.net/4Hdme/
edit: In the case that your property is a number, you must convert it to a string before attempting to slice it:
var propertyAsString = item.TYP_PCON_START.toString();
propertyAsString.slice(-2) + '.' + propertyAsString.slice(0, 4);
> '12.2012'
http://jsfiddle.net/4Hdme/1/
var a = item.TYP_PCON_START,
a = a+"",
a = a.split("");
a.splice(2,0,".");
a = a.join("");
a = parseFloat(a);
console.log(a);
Related
I am trying to convert present time to hexidecimal then to a regular string variable.
For some reason I can only seem to produce an output in double quotes such as "result" or an object output. I am using Id tags to identify each div which contains different messages. They are being used like this id="somename-hexnumber". The code if sent from the browser to a node.js server and the ID is split up into two words with first section being the person's name then "-" is the split key then the hexidecimal is just the div number so it is easy to find and delete if needed. The code I got so far is small but I am out of ideas now.
var thisRandom = Date.now();
const encodedString = thisRandom.toString(16);
var encoded = JSON.stringify(encodedString);
var tIDs = json.name+'-'+encoded;
var output = $('<div class="container" id="'+tIDs+'" onclick="DelComment(this.id, urank)"><span class="block"><div class="block-text"><p><strong><'+json.name+'></strong> '+json.data+'</p></div></div>');
When a hexidecimal number is produced I want the output to be something like 16FE67A334 and not "16FE67A334" or an object.
Do you want this ?
Demo: https://codepen.io/gmkhussain/pen/QWEdOBW
Code below will convert the time/number value d to hexadecimal.
var thisRandom = Date.now();
function timeToHexFunc(x) {
if ( x < 0) {
x = 0xFFFFFFFF + x + 1;
}
return x.toString(16).toUpperCase();
}
console.log(timeToHexFunc(thisRandom));
I've got a variable called colorHM:
var colorHM = "50,50,74,255,100,255,4,3,50".
Now I'm using this snippet here to cut it into pieces with the following scheme: R,g,b,R,g,b,R,g,b, and again to var a = R,g,b and var b = R,g,b etc...
var firstColorHM = colorHM.split(",", 3);
firstColorHM = firstColorHM.toString();
var firstColorHMA = firstColorHM.split(",");
firstCMA.push(firstColorHMA[0]);
firstCMA.push(firstColorHMA[1]);
firstCMA.push(firstColorHMA[2]);
But using it to calculate distance = eDist(firstCMA, firstCTA) gives me NaN.
function eDist (col1, col2) {
var rmean = ((col1[0] + col2[0]) / 2);
var dR = (col1[0] - col2[0]);
var dG = (col1[1] - col2[1]);
var dB = (col1[2] - col2[2]);
return Math.sqrt((2 + (rmean / 256)) * Math.pow(dR, 2) + (4 * Math.pow(dG, 2)) + ( 2 + ((255- rmean) / 256)) * Math.pow(dB, 2));
}
Using firstCMA.push(10); firstCMA.push(20); firstCMA.push(30); instead of firstCMA.push(firstColorHMA[0]);.. makes it work again.
The variable firstCTA is left out, but parsed the same way.
I simply checked if the Arrays were working by trying to call different indexes from the array, which worked.
Why does pushing numbers work but pushing firstColorHMA[0] doesnt?
Thanks in advance!
Like the comments says, when you split() a string, the generated array will contain strings:
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
So actually the array firstColorHMA holds numbers as strings, an easy fix to this, will be using the unary plus to cast the array items to numbers, like this:
firstCMA.push(+firstColorHMA[0]);
firstCMA.push(+firstColorHMA[1]);
firstCMA.push(+firstColorHMA[2]);
Or alternatively, use a more explicit logic like:
firstCMA.push(Number(firstColorHMA[0]));
firstCMA.push(Number(firstColorHMA[1]));
firstCMA.push(Number(firstColorHMA[2]));
I use stringify() method in JavaScript to convert a list of objects to a string, but I need to customize the output on the first level ONLY like the following:
[
/*T01*/ {"startX":55,"endX":109,"sartY":0,"endY":249},
/*T02*/ {"startX":110,"endX":164,"sartY":0,"endY":249},
/*T03*/ {"startX":165,"endX":219,"sartY":0,"endY":249},
/*T04*/ {"startX":220,"endX":274,"sartY":0,"endY":249},
/*T05*/ {"startX":275,"endX":329,"sartY":0,"endY":249},
/*T06*/ {"startX":330,"endX":384,"sartY":0,"endY":249},
/*T07*/ {"startX":385,"endX":439,"sartY":0,"endY":249},
/*T08*/ {"startX":440,"endX":494,"sartY":0,"endY":249},
/*T09*/ {"startX":495,"endX":549,"sartY":0,"endY":249},
/*T10*/ {"startX":550,"endX":604,"sartY":0,"endY":249}
]
Now there are other parameters in stringfy() method, replacer and space, can't I use them to format my output like the aforementioned format including:
tabs
spaces
comments
You are not going to get JSON.parse to make that output since it is not valid JSON. But if you want to have something rendered like that, it is a simple loop and string concatenation.
var details = [
{"startX":55,"endX":109,"sartY":0,"endY":249},
{"startX":110,"endX":164,"sartY":0,"endY":249},
{"startX":165,"endX":219,"sartY":0,"endY":249},
{"startX":220,"endX":274,"sartY":0,"endY":249},
{"startX":275,"endX":329,"sartY":0,"endY":249},
{"startX":330,"endX":384,"sartY":0,"endY":249},
{"startX":385,"endX":439,"sartY":0,"endY":249},
{"startX":440,"endX":494,"sartY":0,"endY":249},
{"startX":495,"endX":549,"sartY":0,"endY":249},
{"startX":550,"endX":604,"sartY":0,"endY":249}
];
var out = "[\n" + details.map(function(val, i) {
var id = "\t/*T" + ("0" + (i + 1)).substr(-2) + "*/\t";
return id + JSON.stringify(val);
}).join(",\n") + "\n]";
console.log(out);
Hi Im trying to iterate through a for loop from a long string of words held in a single string(wordlist), within impactjs:
var wordlist3 ="hellwhentrysthisbreaks"
var xc=3;
var word_length = 4;
var words_in_round = 4;
for ( i=0; i<words_in_round; i++){
var num_words = ['wordlist' + xc].length / word_length;
var random = Math.floor(Math.random() * ((num_words+1) - 0 ));
n = Math.round(random / word_length) * word_length;
random_word =(['wordlist' + xc].substring(n,(n+word_length)))
random_words += random_word;
}
The above code works if i define wordlist as a global, but when i made it local num_words is not defined properly and random word throws this object has no method substring ..
My problem is, that since i converted to local variables when i append the string name and call .length it gives me the length of the new name (wordlist3.length = 9) instead of the length of wordlist3 =20 .. also i cant call the method substring on this object ...
['wordlist' + xc].substring
will NEVER work (well, unless it's preceded by another variable, eg. foo['wordlist' +xc].substring). This is because, in Javascript [anything] means "an array of 'anything'", and (as Kendall mentioned) arrays do not have a substring method.
try:
random_word =(('wordlist' + xc).substring(n,(n+word_length)))
instead.
Im trying to get the last string from a URL for example...
http://www.mywebsite/blog/this-post
I want to use jQuery to get 'this-post'
Ive the following...
$('img.gigthumb').each(function(){
var foo = $(this).parent().attr('href').split('/');
$(this).attr('src','/lic/wp-content/uploads/2012/01/'+foo[1]+'.jpg');
})
only it doesn't work and I presume thats because I have multiple '/' within the URL, any idea how to target just the last?
Hope this makes sense!
This is precisely what .pop() is made for:
$('img.gigthumb').each(function(){
var foo = $(this).parent().attr('href').split('/').pop();
$(this).attr('src','/lic/wp-content/uploads/2012/01/' + foo + '.jpg');
});
Don't use the element with index 1 of foo, but the last one:
$(this).attr('src','/lic/wp-content/uploads/2012/01/'+foo[foo.length-1]+'.jpg');
Splitting with "/" will give you the array:
foo[0] = "http:"
foo[1] = ""
foo[2] = "www.mywebsite"
foo[3] = "blog"
foo[4] = "this-post"
If you want to get the last item regardless of the size of the array do:
foo[foo.length - 1]
or as Joseph mentioned: foo.pop()
Following your example you need the last part of the splits:
$('img.gigthumb').each(function(){
var foo = $(this).parent().attr('href').split('/');
var last = foo.length - 1;
$(this).attr('src','/lic/wp-content/uploads/2012/01/'+foo[last]+'.jpg');
})
var foo = $(this).parent().attr('href').replace(/^.*\\/, '');