简体   繁体   English

JS 为各种相似文件返回单一文件类型(例如 'mp4' || 'm4v' = 'MOV')

[英]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.

相关问题 为什么.mov,.mkv和一些.mp4没有在plyr.js中播放 - Why .mov,.mkv,and some .mp4 are not playing in plyr.js 从文件中提取M4V视频章节 - Extracting m4v video chapters from file 如何将 m4v 视频文件嵌入到我的 html 网页中? - How to embed an m4v video file to my html webpage? 如何指定类构造函数的返回类型(例如使用代理)? - How to specify return type of class constructor (e.g. using proxy)? 使用外部JS文件捆绑TypeScript(例如node_modules) - Bundle TypeScript with external JS files (e.g. node_modules) 单个文件VS导入/导出加载的模块中的JavaScript命名空间模块(例如requireJS,es6) - JavaScript namespaced modules in single file VS import/export loaded modules (e.g. requireJS, es6) Vue.js:如果值为空,则不呈现属性(例如:to=&quot;&quot; 或 type=&quot;&quot;) - Vue.js: Don't render attributes if value is empty (e.g.: to="" or type="") 在纯节点 js 中将 mp4 或 Avi 转换为 m3u8 - Convert mp4 or Avi to m3u8 in pure node js 在跨不同文件分割文件(例如html,图像,css,js等)时,HTML和CSS是否应保留在同一域中 - When splitting files (e.g. html, images, css, js etc) accross different files, should HTML and CSS remain on the same domain 节点正确返回错误(例如,验证错误) - Node return errors correctly (e.g. validation errors)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM