Skip to content

How to get all values in a data table

I am trying to get all the values in a JavaScript data table but whenever I try it says, “table.length is not a function” or, “table.size is not a function” (I’ve tried both).

Here is the code I want to use:

const wordcount = {"this", "is", "a", "data", "table"}

console.log(wordcount.size()) // wordcount.size is not a function

I want it to return an integer value to the console. How would I do this?

Answer

const wordcount = {"this", "is", "a", "data", "table"}; is not a valid object.

You can use make it an array and get the array length by using the length property.

const wordcount = ["this", "is", "a", "data", "table"];
console.log(wordcount.length); // 5

Or, create a proper object and get the size of the object’s properties by using Object.keys which returns an array of a given object’s own enumerable property names, and then getting that length.

const wordcount = {
        first: "this", second: "is", third: "a", forth: "data", fifth:"table"
    };
    
    console.log(Object.keys(wordcount).length); // 5