Convert string of variables separated by & into array [duplicate] - javascript

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
I want to grab the variables contained in the url after somebody logs into my Facebook app.
For example:
// window.location = .../#access_token=CTTG4fT3ci...&expires_in=5298
hash = window.location.hash;
data = PARSE(hash);
console.log(data['access_token'] + ', ' + data['expires_in']);
// returns: CAAG4fT3ci..., 5298
Is there a method or function similar to JSON.parse() that would convert "hash" into an array or object?

function PARSE (hash) {
var l, chunk, i = 0, out = {};
var chunks = hash.substr(1).split('&');
for ( l = chunks.length; i < l; i++ ) {
chunk = chunks[i].split('=');
out[ chunk[0] ] = chunk[1];
}
return out;
}
Here's the fiddle: http://jsfiddle.net/VwLJS/

Related

how to separate a string by comma in javascript? [duplicate]

This question already has answers here:
How do I split a string into an array of characters? [duplicate]
(8 answers)
How to get character array from a string?
(14 answers)
Closed 1 year ago.
how to print as follows in javascript ?
if my input is "smart"
then my output could be "s,m,a,r,t"
I've tried by using this logic but i got the output as
s,m,a,r,t,
let a = " ";
str = userInput[0];
console.log(str);
for (let i = 0; i < str.length; i++) {
a = a + str[i] + ",";
}
console.log(a.trim());
You can use the split method in js
var str = "smart";
var res = str.split("").join();
console.log(res)
//if you want it to remain an array don't do the join

Develop function to find all permutations of a sequence of digits [duplicate]

This question already has answers here:
Recursively print all permutations of a string (Javascript)
(11 answers)
Closed 6 years ago.
I need to write a JavaScript function that shows me all the permutations of the digits of the number passed as an argument, but not sure how to do it.
For example, given 1020, it would produce
1020 , 0120 , 0210 , 0102.
Here's a working solution. Hope it helps!
var result = [];
var newArray = [];
function permute(someArray) {
var i, ch;
for (i = 0; i < someArray.length; i++) {
ch = someArray.splice(i, 1)[0];
newArray.push(ch);
if (someArray.length == 0) {
result.push(newArray.slice());
}
permute(someArray);
someArray.splice(i, 0, ch);
newArray.pop();
}
return result;
};
var n = 1901;
var arr = (""+n).split("");
var myResult = permute(arr);
for(var i in myResult){
console.log(myResult[i].join(""));
}

Print all the names of keys stored in localStorage html [duplicate]

This question already has answers here:
Get HTML5 localStorage keys
(15 answers)
Closed 7 years ago.
I'm trying to print the names of all the keys stored in localStorage with each key in a separate line. This is my code:
function viewsaved(){
$('#saved').show();
var stuffsaved = Object.keys(localStorage);
var splitit = stuffsaved.split(',');
for (var i = 0 ; i < splitit.length ; i++ ){
$('#saved').append(splitit[i]+"<br>");
}
}
when I call the function, it does nothing.
How do you do this properly?
Object.keys returns an array, not a string. Just modify slightly:
var stuffsaved = Object.keys(localStorage);
for (var i = 0 ; i < stuffsaved.length ; i++ ) {
$('#saved').append(stuffsaved[i]+"<br>");
}
If you have or expect a lot of keys, I would suggest building the list in a temporary variable first to avoid frequent DOM update, for example:
var keys = Object.keys(localStorage);
var list = "";
for (var i = 0 ; i < keys.length ; i++ ) {
list += keys[i] + "<br>";
}
$('#saved').append(list);

Why this replace doesn't work Javascript? [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
I want to know why this procedure doesn't replace words
I have to do a procedure which reads a string and replace all word like this {{employee.Name}} into a value on the ticket's scope
var mySplitResult = Val.split(' ');
for (var i = 0; i < mySplitResult.length; i++) {
if (mySplitResult[i].match("{{") && mySplitResult[i].match(".")) {
var start = mySplitResult[i].lastIndexOf(".") + 1;
var end = mySplitResult[i].indexOf("}}");
var result = mySplitResult[i].substring(start, end);
for (var key in ticket.PNData) {
if (key == result) {
change.replace(mySplitResult[i], ticket.PNData[key]);
alert(change)
}
}
}
}
In JavaScript strings are immutable which means you must assign the result to a variable.
mySplitResult[i] = mychange.replace(mySplitResult[i], ticket.PNData[key]);

To Get the query string value from Javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have a code but it is not running properly.
function QueryStringParam1(d) {
var vars = [], hash;
var q = document.URL.split('?')[0];
if (q != undefined) {
q = q.split('&');
for (var i = 0; i < q.length; i++) {
hash = q[i].split('=');
vars.push(hash[1]);
vars[hash[0]] = hash[1];
}
}
return vars[d];
}
Most likely you are trying to get query string value from this query. So you would be getting string having its value after '?' char.
So after split relevant string should be in 1 index not 0..
replace
var q = document.URL.split('?')[0];
by
var q = document.URL.split('?')[1];

Categories