0
Let object = {name:"somename"};
Let objectKeys = Object.keys(object);
Let totality = `${object}.${objectKeys[0]}`;
Console.log(totality);

Desired result is "somename"

I'm writing a Chrome extension that scrapes data off of a website, and exports to csv. I plan to have the user specify what fields they want to export so this isn't necessary really, but I thought it would be a fun learning experience. I don't know how to word exactly what it is I am trying to do, so research is difficult.

I'm not experiencing any errors, and this isn't as much a code snippet as it is something i quickly typed on my phone to get the idea by as to what I'm trying to do, and it is exactly the dynamic thing, thank you!

  • 2
    The first thing you need to do is look at the errors your code is producing. `let` and `console` are all lower case. – Heretic Monkey Feb 06 '19 at 23:31
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Heretic Monkey Feb 06 '19 at 23:31

1 Answers1

1

This works:

let object = {name:"somename"};
let objectKeys = Object.keys(object);
let totality = `${object[objectKeys[0]]}`;
console.log(totality);

Use the object['key as string'] syntax, example: {x:10}['x'] evaluates to 10.

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84