JavaScript : Printing Dynamic value/data - javascript

This is sample JSON I got {"valueofa" : 1234}.To print the value it is something like body.valueofa. However, this valueofa can be anything like apple or something else. So to parse that I object I had tried with ${body}.${value} which isn't working and it's shouldn't be. How can I set the variable with body. So that I can parse the body whatever the value is.

If your object always contains only one key value pair like shown in the question, and you don't care about the key, you can just use Object.values()
console.log(Object.values({"valueofa" : 1234})[0]);
console.log(Object.values({"apple" : 1234})[0]);
The same is true for objects holding multiple key value pairs, Object.values() will return an array of all the values in your object if you omit accessing the first element with [0]

You can access a value of JSON using square brackets.
var value = "valueofa";
body[value];

Related

In javascript how to replace elements of an array to it's corresponding values in another object?

I have the mapping of abbreviation and full name as follow:
object = {"TSLA":"TESLA INC.","GOOG":"GOOGLE INC.","APPL":"APPLE INC.","AMZN":"AMAZON CORPORATION", "MSFT":"MICROSOFT CORPORATION"}
as an output of function i get the following array:
array = ["AMZN","APPL"]
I want to display the values of the associated keys as follows:
output_array = ["AMAZON CORPORATION", "APPLE INC."]
Note that, the actual object has more key: value pairs
Using IndexOf() I can replace one value. However, I believe there must be a way of mapping the array elements to replace the values at once.
Since I am new to JS, I would appreciate any suggestion.
array.map(abbr => object[abbr]) does the job; it iterates over the array rather than the object, which is more efficient as the object already is a lookup table with the right keys - you are right, there is no need to use indexOf which uses a linear search.

find value in complex object javascript

Basically I have a complex object that retrieves the GPT API (google publisher tag) with this function:
googletag.pubads().getSlots();
The object value is something like this:
I need to know if there is a way to compare the value of each property with an X value without getting a problem of recursivity (because the object is huge and i need to to that validation several times)
Also, I tried to convert that object into a JSON with JSON.stringify(), and then tried to get the value with a regex, faster, but with this option, I have the problem with Cyclic Object Value.
Any suggestions ?
it's more simple. try it to convert it into an array and later use a filter for comparative with your value.
var objGoogle = {};
var arrayObjectGoogle = [objGoogle];
var filter = arrayObjectGoogle.filter(function(obj){
obj.yourAttr == yourValue; });
this will give you a second array with the values found it. later, index the array for pick up the value do you need.

Get Value of Child Object with JavaScript

I have a JSON collection produced from an object graph. Shown below is an example value. I am having trouble accessing the nested 'Type' object to retrieve any of it's values.
[{"Id":1,"Name":"My Name","Type":{"Id":1,"Name":"my Value"}}]
I am using a JS component that has a property that can be assigned a value similar to below.
myProperty: Type.Name, //Not working
Can someone recommend how I set this value?
What you have is a JavaScript array, not an object, and certainly not JSON. So if you have
var arr = [{"Id":1,"Name":"My Name","Type":{"Id":1,"Name":"my Value"}}]
you'd need to index it, and grab the Type object off of that.
var typeName = arr[0].Type.Name;

How to access object of array?

I am new to jquery and trying something and got stuck at it,
My problem is i have object with array in it i am not able to find the way to access that array from the object
//My object is shown in debugging time is as below
cache:object
0001-:Array[2]
0:value1,
1:value2
_prto_:object
and i want to access the value1 and value2 from the 0001- array from that object is there way to access that array. Any help would be great. I know with $.each i can loop through it and and then again access the array but is there any other way to do it.
You can access it like, and keep in mind that you should use bracket notation in this context, since your keys having a starting character as a number.
cache['0001-'][0] //first element on that array
cache['0001-'][1] //second element
A workaround for your new requirement,
var cache = {'0001-' : [0,1]};
var xKeys = Object.keys(cache);
console.log(xObj[xKeys[0]][0]);
console.log(xObj[xKeys[0]][1]);

Read and loop through an object with non-unique key value pairs

I have an object which contains some key/value pairs. When there is a key/value pair that shares the same key as another key/value pair, the first one is not recognised when I console log the object.
For example:
var test = {
"same" : 'Value1',
"same" : 'Value2',
"different" : 'Value3'
};
console.log(test);
Results in the console as:
Object { same="Value2", different="Value3"}
Is it not possible to read an object that has similar key names?
I am trying to loop through the object using this method (How do I loop through or enumerate a JavaScript object?) but I can only ever retrieve one the key/value pairs that share a key.
An object can not have duplicate keys.
So the reason that you can't read the duplicate keys from the object, is that they were never added as two items in the object in the first place. One of the items will simply overwrite the other.
Could you change the structure of the JSON if needed? JSON objects cannot have duplicate keys. Think of it as a hashmap or dictionary. Depending on the language and JSON parser you may also get an exception (not in Javascript though)
In your example above either change it so that you have unique keys or change ti to an array of values like:
var test = {
"same" : ['Value1', 'Value2']
"different" : 'Value3'
};
console.log(test);
A key is a unique value that uniquely identifies the element within the array/object. So, the answer is no, you can't have two elements with the same key value.

Categories

Resources