React - Cant access property of an object when it is clearly there - javascript

Still hoping someone help me with guidance.
I am newbie learning by doing, not able to access an array property inside an object when it is clearly there as shown from same console log output.
The array property of the object is:
console.log("vCompanyStakeholders",vCompanyStakeholders, "vCompanyStakeholders['buyer']",vCompanyStakeholders['buyer'],"vCompanyStakeholders.buyer",vCompanyStakeholders.buyer );
Console output (as shown in image too):
vCompanyStakeholders {}buyer: (4) [{…}, {…}, {…}, {…}]proto: Object vCompanyStakeholders['buyer'] undefined vCompanyStakeholders.buyer undefined
Console
Background:
I am initialising following in state : "companyStakeholders" : {},
Then in a separate function I am getting data using an API and inserting a few arrays in the above field. buyer (in the above question) is one of the arrays populated.
Then just before the above statement, in my question, I am saving the companyStakeholders in the variable vCompanyStakeholders as follows "const vCompanyStakeholders = this.state.companyStakeholders;"
3> My intention is to use the buyer array by sending it as parameter to another function after the above statement but the array is coming as undefined. Hence using console.log for debugging.
As we can clearly see from the console.log that the buyer array exist. I am confused why I cant extract it into its own variable for further use. I understand being a newbie I am making some very silly mistake

Console.log(vCompanyStakeholders.buyer['PROPERTY IN BUYER YOU WANT TO LOG'])

Related

Why we use console.table method?

I know one of the most basic debugging tools in JavaScript is console.log(). The console comes with several other useful methods that can add to a developer’s debugging toolkit.
but
I'm pretty confused about console.table() method. Is anyone explain?
The Console Object:
The console object gives you access to the browser’s console. It lets you output strings, arrays, and objects that help debug your code. The console is part of the window object, and is supplied by the Browser Object Model (BOM).
There are four different ways of outputting a message to the console:
log
info
warn
error
while
there is another method that is:
console.table()
let me explain
The table method displays an array or object as a table.
console.table(['Javascript', 'PHP', 'Perl', 'C++']);
example:
const superhero = {
firstname: 'Peter',
lastname: 'Parker',
}
console.table(superhero);

Object property displayed in the console, but returns undefined when I read it

article is an object variable.
console.log("reesult id " + JSON.stringify(article));
When I do this, it outputs me: [{"Id":43}]
and when I do:
console.log(article[0]);
It outputs me {"Id":43}
but now... HOW to get just 43?
Because when I type:
console.log(article[0].Id);
, It returns me undefined instead of 43. Pf.
So, HOW to get 43?
It is very difficult because I made researches and it does not work as well.
I am unable to comment to so posting as an answer.
Your solution looks fine.
Could you print the output for
JSON.stringify(article[0].id)
Sequelize has it's own way how to return objects in json so you should try using their way to handle with objects since you are using that library.
This is from Sequelize documentation.
For any response object that resulted from a query, you can extract
only the data you want by appending .get({plain:true}) the the
response.
Here you have a link to that documentation so you can read more for that since you are not providing any code to us.

Access $key from object returned from firebase

I have an object downloaded from firebase which is of type Bug (Custom type). However, when I tried to console log the output with the following code
console.log('bug',this.bug);
I get the following result. It is what I expected where I can go on and do things like
console.log('company', this.bug.companyName)
However, I want to get that key value as well. Howe can I do that? I tried to do
console.log('company', this.bug.key)
console.log('company', this.bug.$key)
and both of them does not work
try to run
console.log('company', this.bug["$key"])
remember property lookup on objects is either
obj.someKey // or
obj["someKey"]

Javascript map string lookup. Can't find key even though key is in object

I must be missing something pretty standard here.
I have a map mapping string to object. What I write out:
console.log(playerMap, this.id(), playerMap[this.id()]);
What it prints out:
this does not make a lot of sense to me since the key seems to be right there.
Fiddle
Based on your Fiddle, apparently your playerMap is not yet initialised when you new FeedViewModel(), if you change your console.log to
console.log(Object.keys(playerMap).length, this.id(), playerMap[this.id()]);
you will see the object is empty
> 0 "123243df6" undefined
It's always confused when you console.log object because the object displayed is only resolved when you expand the > in the console. It is not the state of the object when you console.log'd the object.

How to print an object on the developer tools console in Javascript

Please help me in printing the complete object using console.log method. If I am simply putting the name of the object it simply prints [object] [object]. But I want to print the object with complete hierarchy.
For an example, I am getting object in the following method,
getObject : function(responseObj) {
console.log('Object hierarchy is'+responseObj)
}
This simply returns [object] but I want to see the complete hierarchy in the developer tools. Please see, I am using sencha.
Use console.dir, and don't concatenate...
console.dir( responseObj );
Or if you want a label, use a comma to pass multiple arguments.
console.log('Object hierarchy is:', responseObj)
The problem here is that you are concatenating an object onto a string, so it's not doing what you are expecting. Instead of putting it all into one console.log call, do two, the first with the text you want, and the second with just the name of the object in it.
getObject : function(responseObj) {
console.log('Object hierarchy is:');
console.log(responseObj);
}
edit:
If you are logging into a text/non-interactive console, that doesnt let you explore the object, you'll need to implement a custom inspection function, something like this gist will get you started. What this is doing is echoing out the property name, and the value it finds step by step, but pay attention to what it says there about scoping.
edit edit:
didn't know about console.log taking multiple parameters, never needed it :o handy though!

Categories

Resources