简体   繁体   English

如何在 JavaScript 中将变量转换为字符串?

[英]How do you turn a variable into a string in JavaScript?

I have been searching around and in most forums the reply to this question is more or less that it's unnecessary to convert a variable into string.我一直在四处搜索,在大多数论坛中,对这个问题的回答或多或少是没有必要将变量转换为字符串。 However given the following scenario how could I go about solving the problem?但是,鉴于以下情况,我该如何解决问题?

I have an object categories as follows:我有一个 object categories如下:

const categories = {
  1: ctgOne,
  2: ctgTwo,
  3: ctgThree,
  4: ctgFour,
};

These categories correspond to objects which contain some arbitrary data:这些类别对应于包含一些任意数据的对象:

const ctgOne = {
  test1: ["blah", "blah", "blah"],
  test2: ["blah", "blah", "blah"],
};
const ctgTwo = {
  test1: ["blah", "blah", "blah"],
  test2: ["blah", "blah", "blah"],
  test3: ["blah", "blah", "blah"],
};
const ctgThree = {
  test1: ["blah", "blah", "blah"],
  test3: ["blah", "blah", "blah"],
};
const ctgFour = {
  test2: ["blah", "blah", "blah"],
  test3: ["blah", "blah", "blah"],
};

I have another object categoryList containing some numbers associated to each category in arrays:我有另一个 object categoryList包含与 arrays 中的每个类别相关的一些数字:

const categoryList = {
  ctgOne: [1, 3, 5, 8, 13],
  ctgTwo: [23, 4, 78],
  ctgThree: [12, 33, 54, 8, 13, 12, 45],
  ctgFour: [1, 35, 5, 2],
};

When I run the following script, I get an error for the second console.log(count);当我运行以下脚本时,第二个console.log(count);出现错误。 for obvious reasons because in count = categoryList[categoryName].length;原因很明显,因为在count = categoryList[categoryName].length; "categoryName" is required to be a string. “categoryName”必须是字符串。 However, if I convert the categories in my categories object to strings, console.log(keys);但是,如果我将categories object 中的类别转换为字符串,则console.log(keys); gives me the length of the string instead of the number of keys in the object.给我字符串的长度,而不是 object 中的键数。

let categoryNumber = 1;
let categoryName = categories[categoryNumber];

let keys = Object.keys(categoryName).length;

console.log(keys);

count = categoryList[categoryName].length;

console.log(count);

What would be the best approach to solve this dilemma?解决这一困境的最佳方法是什么? I want to count the number of keys in the object as well as the numbers in the categoryList .我想计算 object 中的键数以及categoryList中的数字。

In your object, why dont you store the object name, which is a string.在您的 object 中,为什么不存储 object 名称,它是一个字符串。

In your categoryList, use the name, instead of the objects, as keys.在您的 categoryList 中,使用名称而不是对象作为键。

You don't get variable names as strings.您不会将变量名称作为字符串。 At best, you'll get to do it the other way round, but really you should never care about variable names as data.充其量,你会反过来做,但实际上你永远不应该关心变量名作为数据。 You can (and should) inline those 4 const variables in your categories object.您可以(并且应该)在您的categories object 中内联这 4 个const变量。

To get the respective value from the other object, use the same keys in both categories and categoryList .要从另一个 object 获取相应的值,请在categoriescategoryList中使用相同的键 That is, either也就是说,要么

const categories = {
  ctgOne: { test1: …, test2: …},
  ctgTwo: { test1: …, test2: …, test3: …},
  ctgThree: { test1: …, test3: …},
  ctgFour: { test2: …, test3: …},
};
const categoryList = {
  ctgOne: [1, 3, 5, 8, 13],
  ctgTwo: [23, 4, 78],
  ctgThree: [12, 33, 54, 8, 13, 12, 45],
  ctgFour: [1, 35, 5, 2],
};

or或者

const categories = {
  1: { test1: …, test2: …},
  2: { test1: …, test2: …, test3: …},
  3: { test1: …, test3: …},
  4: { test2: …, test3: …},
};
const categoryList = {
  1: [1, 3, 5, 8, 13],
  2: [23, 4, 78],
  3: [12, 33, 54, 8, 13, 12, 45],
  4: [1, 35, 5, 2],
};

but not a mix of them.但不是它们的混合。 If you really need both of them, you won't get away without a separate mapping table.如果你真的需要他们两个,你就离不开一个单独的映射表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM