0

I have a problem with creating a dynamic "string" passed intp Object.values()

import data from "~/static/words.json"
console.log(Object.values(data.vocabulary.lection1)) //This works
let section = "vocabulary"
let lection = "lection1"
let string = `data.${section}.${lection}`
console.log(Object.values(string)) //This doesn't work, It returns ["d", "a", "t", "a", ".", "v", "o", "c", "a", "b", "u", "l", "a", "r", "y", ".", "l", "e", "c", "t", "i", "o", "n", "1"]
  • You can use `Object.values(data[section][lection])`. The code you have is not how JavaScript works. – Pointy Aug 19 '21 at 17:54
  • @Pointy But there is a problem, I got this error. Element implicitly has an 'any' type because expression of type 'string' can't be used to index type. –  Aug 19 '21 at 17:58
  • 2
    @devMontu that is a TypeScript error. – VLAZ Aug 19 '21 at 18:01
  • @VLAZ so how can i fix that? I cant? –  Aug 19 '21 at 18:04
  • @devMontu use a type guard or an assertion, if needed. I neither know your data, nor the types you're working with, so I cannot really be specific. – VLAZ Aug 19 '21 at 18:50

3 Answers3

0

You're turning it all into a string then breaking it down into an array by misuing Object.values(). This is likely what you're looking for:

UPDATED after additional info:

const data = { vocabulary: { lection1: { hello: 'helllo' } } };
console.log(Object.values(data.vocabulary.lection1)); //This works
let section = 'vocabulary';
let lection = 'lection1';
let string = `data.${section}.${lection}`;
const otherString = data[section][lection];
// console.log(Object.values(string));
console.log(Object.values(otherString));
Dmitry Kostyuk
  • 1,354
  • 1
  • 5
  • 21
0

The issue is that you you are passing a string literal into Object.values().

As you can see on the documentation, Object.values() takes an iterable (a string, for example, is an iterable list of characters) and splits it into an array.

The issue is that you are trying to access the value of an object, but you aren't referencing the accessed dictionary whatsoever, just a String. In fact, Object.values() is not needed whatsoever in this case.

You can access the values of an object in two ways.

data.section.lection1 is one option, where section and lection are key names in the object. This is not dynamic, however useful to know. The other way, which is more likely what you're looking for, is data[selection][lection] where selection and lection are variables representing strings that are valid keys in the Object (or the corresponding type for the keys).

Here's a fiddle I made for it: https://jsfiddle.net/isaackogan/p1dxknwg/1/

Isaac K
  • 38
  • 6
0

Object.values() return the values of an object as a two dimensional array

let sampleObject = { a: 'ab', b: 'ba'}
console.log(Object.values(sampleObject)) // ['ab', 'ba']

Now, String also behaves like an object in JS. So, when you passed the string in Object.values it returned the array of characters that makes up the string.

Give the documentation a shot: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values