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);
Related
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]
I have to add a property for an object returned from my db before pushing it to an array. Changing properties work, but adding new ones doesn't. Can anyone can explain the logic behind this behavior?
ids.forEach(async (id, index) => {
//get object without highlight property
let real_a = await database.getA(id)
real_a.highlight = "shoud add highlight property to real_a object"
realItems.push(real_a)
// the correct string is printed
console.log(real_a.highlight)
//object still doesn't have that property
console.log(real_a)
}
It was the intended behavior.Sorry for bothering.
I was using a mongodb query for the database.getA(id) function and it turns out you have to specify a parameter in the mongodb query to get an actual changeable JSON object.
Here is the complete answer:
Why can't you modify the data returned by a Mongoose Query (ex: findById)
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.
I try to read my data on a Hash Table but I search in the internet but i don't find a solution.
KPIs.push( {name: [data[0][j]], unite :[data[1][j]], order: [data[2][j]], column:[j] , area:[getArea(data[0][j])] } ) ;
I try :
KPIs.value["name"] // doesn't work
KPIs.length // work
How can I read this HashTable ?
Thanks for your help.
Based on your code it appears you are pushing an Object onto an Array, but you attempt to access the object properties directly on the Array, rather than on the element in the Array.
You'll first need to access the correct Array element, before attempting to access your object properties:
KPIs[0].name
or, to loop over them:
for(var i in KPIs){
var name = KPIs[i].name;
Logger.log(name);
}
See details on Arrays here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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]);