How to return this property value? - javascript

Using console.log(instance.element);
Returns all these values:
The problem is the values are inside the 0 group. I need to return clientWidth value because I want to compare it with another value that I have.
I tried this:
console.log(instance.element.clientWidth) and returns undefined which I believe it's because I'm not accessing correctly.
Using Google Chrome inspector right click, I can copy the property path which gives me this: [""0""].clientWidth
I tried then: console.log(instance.element.[""0""].clientWidth)
But my Javascript editor throws me error on that sentence.
Any ideas?

it is an object and it is not array
if you want to access its element you can use
var dataarray = Array.from(instance.element)
and then you can access dataarray [0]

Related

JavaScript : Printing Dynamic value/data

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];

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.

Cant access single object in array?

Hi I am having problems with accessing Object in array... I dont know is it because i updated Chrome or because i added, and after removed Preact from my React application. Problem is this:
Tags is array of objects:
var fullTag = tags.filter(tag => tag.tagId==tagId);
console.log(fullTag);
And as a result i get this in console:
[{…}]
When i expand it i get this:(image)
So there's no way to access it except with
console.log(Object(fullTag[0]).tag);
In all other ways i get undefined... Why is this?! I can swear that i could access it with fullTag.tag until yesterday... Can someone explain me please?
The filter() method creates a new array with all elements that pass the test implemented by the provided callback function.
So, after you filter an array, you will obtain another array even if there is only one item which pass the test function. That's why you couldn't access it using fullTag.tag.
Solution is to access one element using its index.
let tags=[{"id":1,"tag":"tag1"},{"id":2,"tag":"tag2"}];
let tagId=1;
var fullTag = tags.filter(tag => tag.id==tagId);
console.log(fullTag);
console.log(Object(fullTag[0]).tag);
If tagId property is unique in your array you can use find method.
var fullTag = tags.find(tag => tag.id==tagId);
Now you can access your tag property in that way you wished.
console.log(fullTag.tag);

Use value from array as property

So I have a dictionary-like object and an array:
var colors = {"b":color(0, 0, 0)};
var ar=[["b","0","0"],["b","b","0"],["b","b","b"]];
Now, I would like to get the value from the dictionary using the array like so:
colors.ar[0][0]
Which should give me the color black. However, this gives me an error:
Cannot read property '0' of undefined
I believe this is because it's trying to access colors."b" instead of colors.b .
So how can I get the property from the dictionary by using a value from the array?
Thanks!
I get it. What you want is this:
colors[ar[0][0]]
Since ar[0][0] resolves to "b", colors[ar[0][0]] resolves to colors["b"].
If you use dot notation it will try to access colors.ar which is undefined.

javascript object appears null but object.property returns data

I am trying to set a object var but for some reason this object will not allow me to set a variable, but it still shows up.
Theme.opts.series = series_data.series;
console.log(Theme.opts);
console.log(Theme.opts.series);
In the above example the first console log returns an object but series is null.
The second console log returns the contents of series_data.series as expected.
Why would it appear to be null when the other says that it is there?
Let me know if you need more info. Series holds arrays that are 1000+.
I can also set this to any other property except series. So Theme.opts.foo = series_data.series works as expected.
You need to define Theme.opts first:
Theme.opts = {};
Theme.opts.series = series_data.series;

Categories

Resources