grab text from html and use it and concatenation to access object property

I don’t know if this is possible, and I’m pretty new to JavaScript and programing, but here is what I’m trying to do:

I have a table in HTML:

<table>
    <tr>
      <td id="w1t1">kc</td>
      <td id="w1s1">30</td>
    </tr>
</table>

I have an object in JS:
const kc = {name: kc, color: "red", score: 0};

I would like to grab the text like so:
let team1 = document.getElementById("w1t1").textContent;

this should set the value of team1 to "kc".
I would then like to concatenate with ".score" to access kc.score like so:

function test(){
    let t1 = team1 + ".score";
    console.log(t1);
}

I would like the value of t1 to be 0, which is the value of kc.score, but instead it’s value is "kc.score".

is there anyway to get this to work?

>Solution :

As far I understand you want the score by team1 so here is how you can do this

// First store name of the team in a variable like you are doing
const team1 = document.getElementById("w1t1").textContent;
// now in team1 var we have value of kc

// modify your object a bit
const  team = { kc: {name: kc, color: "red", score: 0} };

console.log(team[team1].score)

Let me know if this works!

Leave a Reply