This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
I have a form that uses the get method and contains an array:
http://www.example.com?name[]=hello&name[]=world
I'm trying to retrieve array values 'hello' and 'world' using JavaScript or jQuery.
I've had a look at similar solutions on Stack Overflow (e.g. How can I get query string values in JavaScript?) but they seem to only deal with parameters rather than arrays.
Is it possible to get array values?
There you go: http://jsfiddle.net/mm6Bt/1/
function getURLParam(key,target){
var values = [];
if (!target) target = location.href;
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var pattern = key + '=([^&#]+)';
var o_reg = new RegExp(pattern,'ig');
while (true){
var matches = o_reg.exec(target);
if (matches && matches[1]){
values.push(matches[1]);
} else {
break;
}
}
if (!values.length){
return null;
} else {
return values.length == 1 ? values[0] : values;
}
}
var str = 'http://www.example.com?name[]=hello&name[]=world&var1=stam';
console.log(getURLParam('name[]',str));
console.log(getURLParam('var1',str));
Related
This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
I have this data
var selectedValue = [];
selectedValue.push({0:'Data A'});
selectedValue.push({1:'Data B'});
I want to check if my new data is already exists in that array. I'm trying to use includes()
function inArrayCheck(val) {
console.log(selectedValue.includes(val));
}
Then i try another way
function inArrayCheck(val) {
if (Object.values(selectedValue).indexOf(val) > -1) {
console.log(val);
}
}
both of them returning false when i input Data A
Objects will not be equal unless they have the same reference, even when they have the same key/value pairs. You can do the comparison after converting the objects to string using JSON.stringify with limited capability, like the order of elements in the object and the case of strings matters:
var selectedValue = [];
selectedValue.push({0:'Data A'});
selectedValue.push({1:'Data B'});
function inArrayCheck(val) {
return selectedValue.some(obj => JSON.stringify(obj) === JSON.stringify(val))
}
console.log(inArrayCheck({0:'Data A'}))
You are trying to find a value from an object which is inside an array. You can try like so:
var selectedValue = []; // this is an array
selectedValue.push({0:'Data A'}); // here you push an object to the array
selectedValue.push({1:'Data B'}); // to find the value later you need to find it inside the object!
// above three lines can also be written like so and are the same
// var selectedvalue1 = [{0:'Data A'}, {1:'Data B'}];
function inArrayCheck(val) {
selectedValue.forEach(function(element){
if (Object.values(element).indexOf(val) > -1) {
console.log('found value: ' + val);
}
});
}
inArrayCheck('Data A');
if you want to use includes you need to have an array like so:
var selectedValue = [];
selectedValue.push('Data A');
selectedValue.push('Data B');
// above three lines can also be written like so and are the same
// var selectedvalue1 = ['Data A', 'Data B'];
function inArrayCheck(val) {
console.log(selectedValue.includes(val));
}
inArrayCheck('Data A')
You forgot to go trough each value. You can also use find() function, to check if array have a value which sutisfy your condition.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
In Javascript, if you serialize() a key/value array pair, then you'll get something like single=Single&multiple=Multiple. Is there any way to "unserialize" this string to get an array of key/value pairs again? If not, what's the most efficient way?
As answered here: https://stackoverflow.com/a/10126995/183181
var str = 'single=Single&multiple=Multiple';
console.log( getParams(str) );
function getParams (str) {
var queryString = str || window.location.search || '';
var keyValPairs = [];
var params = {};
queryString = queryString.replace(/.*?\?/,"");
if (queryString.length)
{
keyValPairs = queryString.split('&');
for (pairNum in keyValPairs)
{
var key = keyValPairs[pairNum].split('=')[0];
if (!key.length) continue;
if (typeof params[key] === 'undefined')
params[key] = [];
params[key].push(keyValPairs[pairNum].split('=')[1]);
}
}
return params;
}
This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 8 years ago.
I want to pass a string for an array's name to a function, and the function create that array, e.g:
make_array('array_name', data);
function make_array(array_name, data){
array_name = [];
// do stuff
array_name.push(//stuff);
}
I don't want to have to create the array first manually
You can do .
window[array_name] = [];
You can use eval() to do it.
eval("var " + array_name + " = []");
If you just want the function to return an array, there is no need to create it beforehand. You can just do this:
function make_array(data){
var array_name = [];
// do stuff
array_name.push(//stuff);
return array_name;
}
var my_new_array = make_array(data);
This question already has answers here:
Use variable's value as variable in javascript
(2 answers)
Closed 8 years ago.
//Admin.js
var insertAdminFeed = function(s, id, timestamp){
var admin_att_new_key = '12345';
var admin_att_new_key2 = 'abc';
var admin_att_new_key3 = 'zyzyz';
var s = 'admin_att_new_key';
console.log(global[s]); //should print '12345'
};
exports.insertAdminFeed = insertAdminFeed;
I want to convert a string to a variable in node.js (I have many keys, and I don't want to write if/else statements for all of them) How can I do that?
This is not really possible in JavaScript.
You'd usually use an object literal to achieve similar needs.
var key = 'foo';
obj[key] = 1;
obj['foo'];
To be thorough, it is technically possible in JS using eval. But really, don't do this.
eval("var "+ name + " = 'some value';");
eval("console.log("+ name +")");
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
If I have a url https://stackoverflow.com/?variable=12345 how can I check if there is a GET parameter in the URL and what it is equals to in JS or jQuery?
For example in PHP:
if(isset($_GET['variable']))
$variable = $_GET['variable'];
Thanks.
function get_var(var_name){
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == var_name){return pair[1];}
}
return(false);
}
And to use:
var get_variable = get_var("variable");
if (get_variable !== '') {
// Variable exists
}