[英]JS to return a single file type for a variety of similar files (e.g. 'mp4' || 'm4v' = 'MOV')
I have a populateTable function, and I'm trying to combine a couple of different file types that might be read from the JSON file into one display type.我有一个 populateTable 函数,我正在尝试将从 JSON 文件中读取的几种不同文件类型组合成一种显示类型。 In this case mp4 and m4v into MOV — I'm not getting any errors, but everything is turning up undefined in the table.
在这种情况下,将 mp4 和 m4v 转换为 MOV — 我没有收到任何错误,但表格中的所有内容都未定义。
function populateTable() {
function setGenericType(type) {
if(type == 'mp4' || type == 'm4v') {
return 'MOV'
}
}
for (const d of data) {
$("#contents").append(`<tr>
<td>${d.title}</td>
<td>${d.year}</td>
<td>${setGenericType(d)}</td>
</tr>`)
}
}
Any ideas?有任何想法吗? An example of the JSON from data.js:
来自 data.js 的 JSON 示例:
{
"title":"Arc Transition",
"year":2016,
"type":"mp4"
}
You pass the whole data object into your setGenericType
function, but compare the parameter to strings, so they will never match.您将整个数据对象传递给
setGenericType
函数,但将参数与字符串进行比较,因此它们永远不会匹配。 And if there's no match of type, the function returns undefined
.如果没有类型匹配,函数返回
undefined
。
So either treat the parameter of your function as the object you are passing to it因此,要么将函数的参数视为传递给它的对象
function setGenericType(d) {
if(d.type == 'mp4' || d.type == 'm4v') {
return 'MOV'
}
}
Or just pass the type of the object to your function and keep the function as it is或者只是将对象的类型传递给您的函数并保持函数原样
for (const d of data) {
$("#contents").append(`<tr>
<td>${d.title}</td>
<td>${d.year}</td>
<td>${setGenericType(d.type)}</td>
</tr>`)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.