How to compare the data in JSON object to a variable? [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
Hi guys so I was wondering if it was possible to search content in a Json object with a variable?
Example:
var username = "name";
The json object Im getting from an api is in the following format:
{"name":{"id":224463,"name":"name","profileIconId":715,"revisionDate":1465905397000}}
The username is obviously different for everyone. I want to be able to get the id of the user and display it when the user enters their username in a text field.
HTTP.get(url,function(error,result){
var username ="somename";
console.log(result.username.id);
});
That code above gives me undefined but if I put somename where username is then it works fine.

Use bracket notation to access property using a variable
console.log(result[username].id)
// -^- -^-

Related

Retrieving parameter from query string based on another string in Javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
My app in Google Apps Script gets data from a form that I want to process.
The element form is received and contains the different parameters and values.
If I log the element form it looks like {uid=11, tradingname=xxx, email=yyy}
I can pick up the separate values by running form.uid or form.tradingname for example. But what I want is to pick up the parameters from the element form by referring to a String "uid" (because I get this value form the headers row of the spreadsheet).
Is there a simple way I can do this in one line, for example something like:
form.element["uid"]
So far I've only found difficult methods like https://gomakethings.com/how-to-get-the-value-of-a-querystring-with-native-javascript/ and I would think that I should be able to do this in an easier way.
Thanks!
You can use $("#yourform").serializeArray() and then iterate to match "name" values you want

How to escape # in javascript to retrive object property [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
i have Javascript object as below.
How can i get value for SERIAL#, like i can retrieve *.INST_ID or *.INSTANCE.
how can i escape # and get the value required.
So far i have tried SERIAL#23% but none helped so far.
var t = {"INST_ID":"1","INSTANCE":"xina","SID":"27","SERIAL#":"48810", "PROGRAM":"Perl#app01"}
console.log(kSess.SERIAL%23); gives syntax error
I am parsing variable "t"
This data is coming from java code, so there is nothing much i can do to change SERIAL# to something else
Any idea?
Try to retrieve the value like this...
t["SERIAL#"]; // will return you the value..
Store it somewhere or play with it as you like. :)

Jquery is not accepting hypen in the jsp page? [duplicate]

This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 8 years ago.
I have a field sent by SERVICE which is having hypen in it. Eg., first-name (As JSON object)
But when I try to get the value of that field through the jsp. I am getting a script error.
Please let me know how to access the hypen also in this?
var nameList = msg.RESPONSE.DATA.NAME-LIST;
The above way when I try to access it is throwing script error
A variable or property name with an hyphen is indeed wrong in javascript (Jquery).
However, you can access the "problematic" property like this :
var nameList = msg.RESPONSE.DATA["NAME-LIST"];
I would recommend to rename the property(ies)
without hyphen if you control the content of this response

how to get the value of a key from json which matches the user input from dropdown in javascript? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I have json in the following format.
[{"custId":7,"emailId":"raju.allen1888#gmail.com","facebookId":"","twitterId":"","mobilePhone":"","landPhone":"","firstName":"Allen","lastName":""},{"custId":8,"emailId":"raju#gmail.com","facebookId":"","twitterId":"","mobilePhone":"","landPhone":"","firstName":"Emanuel","lastName":""}]
i have a dropdown list with all the keys, if i choose a key, for e.g emailId, i need to get the emailId from the json.
it works fine when i give like this,
for(i in data){
alert(data[i].emailId);
}
when i get the value from the dropdown in a variable and try to use it like the following its giving me error as undefined.
var key = $('#dropdownvalue').val(); //emailId as value from the dropdown
for(i in data){
alert(data[i].key);
}
how to solve this, to select the value for the key selected through dropdown.
Use bracket notation to access the property with a dynamic name :
alert(data[i][key]);
This means go to the object data[i] and access a property whose name is the string value of what's contained in key

How to correctly get JSON value? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I have this json array
var currencyformats =
{"USD":[
{'symbol':'$', 'left':true}
],
"UAH":[
{'symbol':'₴', 'left':true}
],
"EUR":[
{'symbol':'€', 'left':false}
]
};
How retreive '₴' ?
I tried this (in cookie "to" I've "UAH")
currencyformats[$.cookie("to")].symbol
but I've obtained undefined
The problem is that under each country code, you've defined an array with a single object. That means that after you access the country code, you'll need to also access the first index in the array.
So, assuming the cookie is set to the value that you're expecting:
currencyformats[$.cookie("to")][0].symbol;

Categories

Resources