0

I have a variable params inside a function someFunction(),

function someFunction() {
  var params = ""
  params += "type=" + type + "&intro=" + intro + "&val=" + val; // then I add a value on params
  console.log(params.val); // now, how can i do this?
}

Any ideas?

Hard Spocker
  • 765
  • 11
  • 32
  • You can't access it like that... – Arun P Johny Jan 28 '16 at 04:08
  • 2
    Methods you have used are used to access the property of the object by mentioning the key. You have string as an input not `object` – Rayon Jan 28 '16 at 04:10
  • "params" variable consist of the string as value.To get the value of "val " use Slice, Substring, or Substr function. – Domain Jan 28 '16 at 04:12
  • 2
    @RayonDabre The problem is not that `params` is a string, instead of an object, the problem is that property access works on the prototype chain, not on inner contents of a string. That means, `params["val"]` would access the property `val` from `params`, but it’s not an existing property in the given example, it’s just part of the content of the string. `params["split"]`, however, _works_, because `split` is a property on `String.prototype`. – Sebastian Simon Jan 28 '16 at 04:30
  • Possible duplicate of [How to get the value from the URL parameter?](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter) – Sebastian Simon Jan 28 '16 at 04:32
  • @Xufox, Ooh yes! Never looked at it that way.. – Rayon Jan 28 '16 at 04:33
  • @Xufox, the one you are referring is getting the value of url parameter which is completely different approach to mine, which is to access it in another function – Hard Spocker Jan 28 '16 at 04:35
  • @HardSpocker, Can you show us how you are accessing `anotherFunction` and which arguments are being passed ? – Rayon Jan 28 '16 at 04:36
  • @HardSpocker, You are overriding `params`, use different variable name.. – Rayon Jan 28 '16 at 04:45

5 Answers5

2

Your params variable is just a string. What you are trying to do is access it like an object.

A better solution would be:

// Create an object
var params = {};

// Create the properties
params.type = "some type";
params.val = "some value";
params.intro = "some intro";

// Alternatively
var params = {
    type: "some type",
    val: "some value",
    intro: "some intro"
};

// Function that does something with your params
function doSomething(input) {
    console.log(input.val);
}

// Function that converts your object to a query string
function toQueryString(input) {
    var keyValuePairs = [];
    for (var key in input) {
        if (input.hasOwnProperty(key)) {
            keyValuePairs.push(encodeURI(key) + "=" + encodeURI(input[key]));
        }
    }
    return keyValuePairs.join("&");
}

doSomething(params);
console.log(toQueryString(params));

Outputs

some value

type=some%20type&val=some%20value&intro=some%20intro

As a side note, it is generally a bad idea to use words that can be potentially a keyword in your code or in the future (IE: params). A better name would be one that is informative, such as welcomeMessage, introduction or employee (Based off the 3 values you listed)

Community
  • 1
  • 1
JosephGarrone
  • 4,081
  • 3
  • 38
  • 61
2

When it's a string, it's a string. You could parse it to get values based on known format, but there is no way of referencing something inside of the string.

Therefore, it's better to store params in the object for later use and create a string only when you need it, based on that object.

So, it could be like this:

var params = {
  val: val,
  type: type,
  intro: intro
};

then, params.val will be accessible. When you'll need a string, you'd do var string = "type=" + params.type + "&intro=" + params.intro + "&val=" + params.val;

Microfed
  • 2,832
  • 22
  • 25
2

Better to write your custom function. Here what I have tried.

window.onload = function() {

  var params = "";
  var type = "types";
  var intro = "int";
  var val = "values";

  params += "type=" + type + "&intro=" + intro + "&val=" + val;

  var convertToObject = function(strParams) {

    var objQueryString = {};
    var arrParam = strParams.split('&');
    console.log(arrParam)

    arrParam.forEach(function(value, key) {

      var arrKyValue = value.split('=');
      objQueryString[arrKyValue[0]] = arrKyValue[1];

    })

    return objQueryString;
  }

  objParams = convertToObject(params);

  console.log(objParams.val, objParams["val"])

}

Here is Plunker

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • I thank all of you for helping me and giving me answers, but this one gave me solution so I need to put a check here. :) – Hard Spocker Jan 28 '16 at 04:52
0

var params = "" is not a function, its a statement. A function in JS is created by using function(params) = {} or in ES6 you can use =>. When you use += on a sting you are saying "take whatever string is already in params and add on to it what Im about to tell you." After your statement is evaluated you have a new string. Although strings can be accessed like arrays e.g. x = 'hello' and then x[0] would return 'h' where 0 refers to the character at the index of the string. You are trying to use dot notation which is used for JS object literals e.g. x = {prop1: 200, prop2: 300} then x.prop1 would return 200. Or using the array syntax you would do x[prop1] instead.

Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109
0

Do it this way, check demo - fiddle:

var value = false, temp;
params.split('&').forEach( function(item) {
      temp = item.split('=');
        if(temp[0] === 'val') {
            value = temp[1];
        }
})

console.log(value);
Nikolay Ermakov
  • 5,031
  • 2
  • 11
  • 18