Please suggest me how to play with Join in javascript.
My Code
var multiTokenQuery = value.join(',');
submitted.set('multiTokenQuery', multiTokenQuery);
alert(multiTokenQuery);
the above code shows (abc,bcd) but I want it in ('abc','bcd') I mean I need single qoutes on the values ...
Please Help
Change the glue in the bits:
var multiTokenQuery = value != null && value.length > 0 ? "'" + value.join("','") + "'" : value;
You'd like to use the following instead to get your quotes.
var value = ['test','sample','example'];
var multiTokenQuery = "'" + value.join("','") + "'";
alert(multiTokenQuery);
JSFiddle: http://jsfiddle.net/FJUJ9/1/
Related
I need to get field source from JSON but my solution not working !!
this is json:
"trailers":{
"quicktime":[],
"youtube":[{
"name":"BandeAnnonce",
"size":"HD",
"source":"RqEuaM9Fsrg",
"type":"Trailer"
}]
}
i try to get the source :var link_trailer = data.trailers[0].youtube[0].source;
but is not working for me !!
trailers is object, you don't need to use index.
var link_trailer = JSON.parse(data).trailers.youtube[0].source;
Try the following code
var text = '{' +
'"trailers": {' +
'"quicktime": [],' +
'"youtube": [{' +
'"name": "BandeAnnonce",' +
'"size": "HD",' +
'"source": "RqEuaM9Fsrg",' +
'"type": "Trailer"' +
'}]' +
'}' +
'}';
obj = JSON.parse(text);
var source = obj.trailers.youtube[0].source;
document.getElementById("demo").innerHTML ="Source field value is :"+source;
Here is the working jsfiddle:http://jsfiddle.net/NJMyD/5387/
Parse json into object first
var link_trailer = JSON.parse(data).trailers[0].youtube[0].source
Why js window.location is not accept a variable that assigned from json,
I hava checked my json, and the json is exist by console.log(myJsonCallback)
Object { status=1, action="update", id=90}
The code like this,
if(response.action === "update"){
var id = response.id;window.location.href=
"/index.php?r=kasir%2Ftransaction%2Fview&id=id"}
in browser, little funny coz it gives me :
http://10.60.36.79/index.php?r=kasir%2Ftransaction%2Fview&id=id
Please advice
UPDATE
My original code like this :
. 'if(response.action === "update"){'
. ' var id = response.id;'
. 'window.location.href= "' . Url::toRoute(['view']) . '&id=${id}"'
. '}'
See, I use dot(.) cos the code is a string on php
Please advice
If what you expected to work would actually interpolate the variable, it would look like this:
/index.php?r=kasir%2Ftransaction%2Fview&90=90
But your string does not actually contain any variables. Try this instead:
if(response.action === "update") {
var id = response.id;
window.location.href = "/index.php?r=kasir%2Ftransaction%2Fview&id=" + id;
}
Or if you are using ES6, you can use template literals
window.location.href = `/index.php?r=kasir%2Ftransaction%2Fview&id=${id}`;
In your code, "/index.php?r=kasir%2Ftransaction%2Fview&id=id", this entire line is a single string without any variable. You will have to concatenate this string with the actual id.
Like this :
if(response.action === "update"){
var id = response.id;
window.location.href = "/index.php?r=kasir%2Ftransaction%2Fview&id=" + id ;
}
Thanks all, my bad,
Perhaps not sleep until morning today.
this is the code
. 'if(response.action === "update"){'
. ' var id = response.id;'
. 'window.location.href= "' . Url::toRoute(['view']) . '&id=" + id'
. '}'
Please, up again
hosts=".uk.com:hostname:#10.10.10.10/10:#[2001:db8:1/64]:#11.11.11.11/11:#[::2/24]"
In javascript, how do i split the above string("hosts") string like the following :
newhosts=.uk.com,hostname,#10.10.10.10/10,#[2001:db8:1/64],#11.11.11.11/11,#[::2/24]"
tried this :
var hosts, newhosts;
var ip6_hosts = [];
var ip6_re = /#\[(.*?)\]/g;
hosts=".uk.com:hostname:#10.10.10.10/10:#[2001:db8:1/64]:#11.11.11.11/11:#[::2/24]";
while ((match=ip6_re.exec(hosts)) != null)
ip6_hosts.push(match[0]);
non_ip6_hosts=hosts.replace(ip6_re, '').replace(/:+/g, ':');
newhosts=ip6_hosts.concat(non_ip6_hosts.split(':'));
actual output :
newhosts=#[2001:db8:1/64],#[::2/24],.uk.com,hostname,#10.10.10.10/10,#11.11.11.11/11
expected output :
newhosts=.uk.com,hostname,#10.10.10.10/10,#[2001:db8:1/64],#11.11.11.11/11,#[::2/24]
but not sure how to preserve the order. is there any way to achieve an expected output ?
You could try:
var openbracket=0;
for (i=0; i<hosts.length; i++)
{
if (hosts.substr(i,1) == '[') openbracket=openbracket+1;
if (hosts.substr(i,1) == ']') openbracket=openbracket-1;
if ((hosts.substr(i,1) == ':') && openbracket==0)
{
hosts = hosts.substr(0,i) + ',' + hosts.substr(i+1,hosts.length-i-1);
}
}
seems to work for me, though I'm not sure if there's a better method for changing the value of hosts. All it needs to do is insert the ',' at the location i. The above code adds everything to the left of the ':', a ',', and everything to the right of the ':'.
note: this assumes you don't want any ':' inside of brackets changed to a comma.
hope this helps.
Can't You just say:
host = host.replace(/:+/, ',');
whenever you want to change it?
I feel like this is too simple of an answer, comment if I'm not getting it.
The following should work:
hosts.replace(/([^:]{1})\:{1}([^:]{1})/g, '$1,$2')
Try this.
var hosts='.uk.com:hostname:#10.10.10.10/10:#[2001:db8:1/64]:#11.11.11.11/11:#[::2/24]';
hosts = hosts.replace(/:#/g, ':##');
hosts = hosts.split(':#');
var hostDetails = hosts[0].split(':');
var newHost = hostDetails.concat(hosts.splice(1, hosts.length));
console.log(newHost);
Can you try this...
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
hosts=".uk.com:hostname:#10.10.10.10/10:#[2001:db8:1/64]:#11.11.11.11/11:#[::2/24]"
hosts = hosts.split(':#').join(',#');
var re = /:\w/g;
var found = hosts.match(re);
hosts.replaceAt(found.index,',');
I want to put two values or data into my span. I am putting value into span like this:
$('#spanid').text(data[i].phonenumber);
the code above will put the phone number to the span now I want to make it in a way that if the data has another number like mobile number i will add it to the text if available. I put it like this:
$('#spanid').text(data[i].phonenumber|| data[i].mobilenumber);
But only the first value is set even if there is a value for mobile number.
Any idea is appreciated
Do you want something like this
$('#spanid').text((data[i].phonenumber ? data[i].phonenumber +" , " :"") + " " +(data[i].mobilenumber? data[i].mobilenumber :"" ))
and you can also shorthand the second condition
$('#spanid').text((data[i].phonenumber ? data[i].phonenumber +" , " : "") + " " +(data[i].mobilenumber || "" ))
|| condition for both object will return only first object if it has value to it. If you wish to use it as separator, you need to concatenate it in both data and then set it to span:
$('#spanid').text(data[i].phonenumber +"||"+ data[i].mobilenumber);
The further answers are MAYBE working, but concatinating strings like that is very slow, better don't do so.
Concatinating a strings is fastest like this:
[string1, string2].join('')
You don't need any operator like || to add in text. Just add-
$('#spanid').text(data[i].phonenumber +""+ data[i].mobilenumber);
So, if any one is present, it will be available in text. Or if both are present it will be in appended form
Fiddle : http://jsfiddle.net/1oo4noed/
With your code the mobile number would only be displayed if there is no phone number. Try it with
$('#spanid').text((data[i].phonenumber || '') + (data[i].phonenumber && data[i].mobilenumber ? ", " : "") + (data[i].mobilenumber || ''));
You can do this by adding a the + operator in the text(); function to concatenate the strings:
$('#spanid').text(data[i].phonenumber + data[i].mobilenumber);
For putting both of the values within the text() function, with checking if they exist, i'd do something like this:
$('#spanid').text((data[i].phonenumber == "" ? "" : data[i].phonenumber) + (data[i].mobilenumber == "" ? "" : data[i].mobilenumber));
In your data object
var data={
phonenumber:222222222,
mobilenumber:''
};
If phonenumber or mobilenumber is missing it should have empty string, null or undefined to identify value exist or not.
Assuming if phonenumber or mobilenumber is missing it will have empty string
var data={
phonenumber:222222222,
mobilenumber:''
};
var text = '';
if(data.phonenumber !='' && data.mobilenumber !='' ){
text = data.phonenumber +','+ data.mobilenumber;
}
else if(data.phonenumber == ''){
text = data.mobilenumber;
}
else{
text = data.phonenumber;
}
$('#spanid').text(text);
Demo
I am trying to add and remove things in a string with using arrays. However this following script I created is not working as it doesn't remove numbers that have been submitted:
function updateCCList(id)
{
var MemberClicked = '[' + id + ']';
var ListClickedMembers = document.frmSendMail.hidSenderList.value;
if(ListClickedMembers.indexOf(MemberClicked) == -1)
{
ListClickedMembers += MemberClicked;
}
else
{
ListClickedMembers = ListClickedMembers.replace(/' + MemberClicked + '/g,'');
}
alert(ListClickedMembers);
document.frmSendMail.hidSenderList.value += ListClickedMembers;
}
Any idea what is wrong?
Many thanks,
Paul
The main problem:
ListClickedMembers = ListClickedMembers.replace(/' + MemberClicked + '/g,'');
The first RegExp there looks bad. I think you mean new RegExp('\\['+id+'\\]')
In case you care about avoiding duplicate entries:
document.frmSendMail.hidSenderList.value += ListClickedMembers;
You don't need += there, = will suffice.