获取JavaScript列表的长度

huangapple go评论85阅读模式
英文:

Get length of a list javascript

问题

在尝试确定 JavaScript 列表的长度时,我注意到它返回的是列表的实际长度,而不是字符串中的项数。允许我提供一个示例。考虑以下列表:

<QuerySet [<Genre: Romance>, <Genre: Science Fiction>, <Genre: Fantasy>, <Genre: Mystery>, <Genre: Thriller>, <Genre: Horror>, <Genre: Historical Fiction>, <Genre: Biography>, <Genre: Autobiography>, <Genre: Memoir>, <Genre: Young Adult>, <Genre: Children's>, <Genre: Literary Fiction>, <Genre: Comedy>, <Genre: Business>, <Genre: Travel>, <Genre: Cookbooks>, <Genre: Poetry>, <Genre: Drama>, <Genre: Fairy-tail>]>

在我的实现中,我以如下方式检索列表,其中 'genres' 表示列表:

<input type="hidden" id="genres_get" value="{{ genres }}" style="display: none;">

在 JavaScript 中获取它:

genres = document.getElementById('genres_get').value;

然后尝试打印长度:

console.log(genres.length)

执行代码后,它不正确地打印出值 414,而预期的长度是 20。

英文:

When attempting to determine the length of a JavaScript list, I noticed that it returns the actual length of the list rather than the number of items within the string. Allow me to provide an example. Consider the following list:

<QuerySet [<Genre: Romance>, <Genre: Science Fiction>, <Genre: Fantasy>, <Genre: Mystery>, <Genre: Thriller>, <Genre: Horror>, <Genre: Historical Fiction>, <Genre: Biography>, <Genre: Autobiography>, <Genre: Memoir>, <Genre: Young Adult>, <Genre: Children's>, <Genre: Literary Fiction>, <Genre: Comedy>, <Genre: Business>, <Genre: Travel>, <Genre: Cookbooks>, <Genre: Poetry>, <Genre: Drama>, <Genre: Fairy-tail>]>

In my implementation, I retrieve the list as follows, where 'genres' represents the list:

<input type="hidden" id="genres_get" value="{{ genres }}" style="display: none;">

Getting it in Javascript

genres = document.getElementById('genres_get').value;

And trying to print the length

console.log(genres.length)

After executing the code, it incorrectly prints the value 414 instead of the expected length, which is 20.

答案1

得分: 0

输入的值是一个字符串,而不是一个数组。如果将数组放入值中,该数组将使用 .toString() 转换为字符串,并使用逗号连接元素。因此,当您执行 genres.length 时,您正在使用字符串的 length 属性而不是数组,因此您会得到字符串中的字符数。

您肯定会想要拆分字符串,然后计算元素的数量。

console.log(genres.split(',').length)

请注意,这实际上不是一个好的方法。为什么您要首先将数据放入隐藏的输入中呢?您不能在组件中保存它吗?

英文:

The input's value is a string, not an array. If you put an array in the value, the array will be converted to string with .toString() which will join the elements with a comma. So when you do genres.length you are using the length property of a String and not an array, so you get the number of characters in the string.

You'd certainly want to split(',') the string and then count the elements.

console.log(genres.split(',').length)

Note that this is not really a good way to do it. Why are you putting the data in a hidden input in the first place? Can't you just hold onto it in your component?

huangapple
  • 本文由 发表于 2023年6月9日 14:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定