-2

For example, this syntax works as I tested it out on MDN.

enter image description here

I am trying to get the total field off of this object here, but I keep getting a bunch of errors.

enter image description here

Here is the object I am getting back. This is shown in advanced rest client.

enter image description here

How do I get the total value? I tried doing people[0].total, but it's not working as it throws errors.

I get this error:

Uncaught TypeError: Cannot read property 'total' of undefined

However, I tried doing this, and this return the correct value...

const helper= () => {
            for (let prop in people) {
                return people[prop].total;
            }
        }
legit98
  • 57
  • 1
  • 7
  • 1
    Very likely it's this: [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/q/4057440) – VLAZ Jun 21 '20 at 18:51
  • 2
    On second thought what's the problem here? – VLAZ Jun 21 '20 at 18:52
  • Do you mean `obj[0].total`? Your "object" is actually an array. – str Jun 21 '20 at 18:54
  • `Object.keys(obj)` returns an array with the key names. – Ameer Jun 21 '20 at 19:01
  • Yes, but even when I do that, it returns it as 0 when it should be 1. – legit98 Jun 21 '20 at 19:02
  • console.log(Object.keys(obj)[0]); <--- this returns 0 – legit98 Jun 21 '20 at 19:03
  • Yes, because your "object" is an array. Use `obj[0].total` or update your question with an example showing what exactly you would expect as a result. – str Jun 21 '20 at 19:04
  • What do you mean that it "should be 1"? It's an array with an object inside it which starts at index 0, not sure what you're expecting? – goto Jun 21 '20 at 19:06
  • @legit98 `Object.keys(obj)[0]` will return a *key*. `Object.keys(obj)` gives you an array with a single element which is the literal string `"0"` - zero as a string. When you extract the one (and only) element from that array, you get said string. – VLAZ Jun 21 '20 at 19:32
  • I updated the thread - take a look. – legit98 Jun 21 '20 at 19:37
  • 1
    You really need to be more specific. What error messages do you get? Also please make a proper [mcve] (i.e. add code, not just screenshots). Did you have a look at the link in the first comment? – str Jun 21 '20 at 19:39
  • Updated the thread. – legit98 Jun 21 '20 at 19:47

2 Answers2

0

I do not know exactly what you are trying to do but say that we have the following object:

const obj = {
    xD: "sdsd"
    abcde: 123,
    some_name: "Hello"
}

Object.keys(obj) // will return ["xD", "abcd", "some_name"]
Object.values(obj) // will return ["sdsd", 123, "Hello"]

Object.keys(obj)[0] // will return "xD"
Object.keys(obj)[1] // will return "abcd"
Object.keys(obj)[2] // will return "some_name"

Do you mean that with Total value:

const obj = {
    abc: 2
    example: 10,
    test: 3
}

Object.values(obj).reduce((v, curr) => v + curr) // will return 2+10+3 = 15
Hexception
  • 722
  • 10
  • 25
0

The second statement is returning a string of your object keys -

Objects have keys and value for instance -

let object = {
    keyOne: '1',
    keyTwo: 2
}

The object above I have called object, it has the keys - keyOne and keyTwo, the have the value '1' and 2

When you run Object.key(object) it returns as a string the names of the keys - in this case that is:

Object.keys(object)

returns

(2) ["keyOne", "keyTwo"]

To print the value of your Object you could use either dot notation i.e.

 object.keyOne
"1"

or bracket notation i.e.

    object["keyOne"]
"1"

Could you add to your question the structure of your object, please i.e. the code of the obj to best asses how to add those together as it seems you have a string as a value which would need be changed to a number.

Thx - W

Wally
  • 705
  • 1
  • 9
  • 24
  • I am querying the sum of something from my express server, then sending it to the client. You can see that the 0 doesn't have quotes around it, so it is a number pretty sure. – legit98 Jun 21 '20 at 19:09
  • It's basically returning an object. I only need to access the 0th index. That index has a field labeled total. I need the value of that field. – legit98 Jun 21 '20 at 19:10
  • Would you be able to try - your_object_name.0.total << if total is the name of the key that will return the value of the key – Wally Jun 21 '20 at 19:21
  • ok - would you be able to provide in your question above the obj structure that you are looking at so I can see what would be correct :) – Wally Jun 21 '20 at 19:36
  • Thank you for the update - can I ask what is the error specifically saying? – Wally Jun 21 '20 at 19:41
  • I added it to the post just now. – legit98 Jun 21 '20 at 19:45
  • I've tried this out in dev tools and I am able to access the Obj and the value using: >>>> oobj[0]['totals'] // returns 10 & >>>> oobj[0].totals // returns 10 - without some more info I'm sorry to say that I'm at a loss - – Wally Jun 21 '20 at 19:55
  • I added a piece of code that works for some reason... Does that help you? – legit98 Jun 21 '20 at 19:56
  • That code is working as braket notation i.e. object[x] allows you to pass a variable into it - you mentioned that your returning this code from a server and the error message is showing that the document is undefined - I'm begging to think this might be an issue with the function not being ASYNC and the JS code running before the return server values are passed..... are you using callbacks ? Async functions or promises to return this data from your server? – Wally Jun 21 '20 at 20:39