0

I am attempting to get a specific value by supplying the literal key in a javascript object. In the image below you see that "filter" is equal to "Approved". This is coming from reimb_status_description in the image below.

Line 6 of the code is where I assign filter to the value.

const filter = Object.values(jsonData[i]["reimb_status_description"]).join("");

What I do not understand is that if I did not end with .join(""), filter would read as "A,p,p,r,o,v,e,d" which is apparently an array of the letters. Could someone please help me understand why the result is an array instead of just a string? Also, is there a better method of extracting the data I am looking for?

enter image description here

function PopulateReimbursementTable(jsonData, appliedFilter)
{
    ClearReimbursementTable();

    for(var i = 0; i < jsonData.length; i++)
    {
        const tr = document.createElement("tr");
        const entries = Object.entries(jsonData[i])

        const filter = Object.values(jsonData[i]["reimb_status_description"]).join("");
        console.log("filter: " + filter)
        for(const [key, property] of entries)
        {
            if(fields.includes(key)){
                console.log(key + "\t" + property);
                const td = document.createElement("td");
                if(key == "reimb_date_submitted" || key == "reimb_date_resolved"){
                    if(property == null)
                    {
                        td.innerHTML = "tbd";
                    }else{
                        var d = new Date(property);
                        let formatted_date = appendLeadingZeroes((d.getMonth() + 1)) + "-" + appendLeadingZeroes(d.getDate()) + "-" + d.getFullYear();
                        //console.log(formatted_date)
                        td.innerHTML = formatted_date;
                    }
                } else if(key == 'reimb_amount'){
                    if(property === null || property === undefined)
                    {
                        td.innerHTML = "tbd";
                    }else{
                        td.innerHTML = formatter.format(property);
                    }   
                }
                else
                {
                    if(property === null || property === undefined)
                    {
                        td.innerHTML = "tbd";
                    }else{
                        td.innerHTML = property;
                    }               
                }

                if(fields.includes(key))
                {
                    tr.appendChild(td);
                }
            }

        }

        if(appliedFilter == "All"){
            reimbTableBody.appendChild(tr);
        }
        else if(filter == appliedFilter){
            reimbTableBody.appendChild(tr);
        }
    }
}
2
  • jsonData[i]["reimb_status_description"] is a string, so if you do Object.values on it, it would be treated as an array and thus you get the values of said array. Commented Apr 3, 2020 at 5:47
  • I'd suggest to learn about filter, map and reduce funtions, they're very helpful. I like this post medium.com/poka-techblog/… Commented Apr 3, 2020 at 5:55

2 Answers 2

2

That is what Object.values does with a String1. So in your case I think you can use jsonData[i].reimb_status_description to retrieve the string value.

1 Why? Because a string is actually a one-dimensional array with elements of the type character.

console.log(Object.values("some string"));

// this would deliver the string though
console.log(Object.values({someString: "some string"})[0]);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I just updated the code with that and it is giving the results I was looking for. I was too focused on the whole Object.entries(jsonData[i]) that I failed to realize that jsonData[i] is in JSON notation.
1

According to MDN doc:

The Object.values() method returns an array of a given object's own enumerable property values

It seems that jsonData[i]["reimb_status_description"] returns string. Any values passed to Object.values is converted to object. String primitive type can be converted to String array-like object (here more details) in javascript. So when you pass string to Object.values it's converted to array-like object which values are individual characters. So in such case Object.values returns an array with individual characters of given string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.