xxx.forEach is not a function(DOM集合--类数组对象转化为数组)

xxx.forEach is not a function(DOM集合--类数组对象转化为数组)

1,错误:Uncaught TypeError: hdList.forEach is not a function

2,错误的原因

原生js获取的DOM集合是一个类数组对象,所以不能直接利用数组的方法(例如:forEach,map等),需要进行转换为数组后,才能用数组的方法!

3,6种解决办法(假如hdList是一个DOM集合)

(1),Array.from()方法

//将hdList用Array.from()方法转换为数组,并用list变量接收
let list = Array.from(hdList);

 

 

(2),用Array.prototype.slice.call(elems)方法转化为数组

 

//hdList转化为数组并用list变量接收
let list = Array.prototype.slice.call(hdList);
//添加点击事件
list.forEach((current,index) => {
	current.addEventListener('click',() => {
		animationFn(index);
	},false);
});

 

 

 

(3),用[ ...elems ]方法转化为数组

 

let list = [...hdList];//用[ ...elems ]方法转化为数组并用list接收
//添加点击事件
list.forEach((current,index) => {
	current.addEventListener('click',() => {
		animationFn(index);
	},false);
});

 

 

(4),用Array.prototype.forEach.call(elem,callback)方法

 

 

//直接对hdList集合进行循环或者map等
Array.prototype.forEach.call(hdList,function(){
//...
})
Array.prototype.map.call(hdList,function(){
//...
})

 

 

(5),用Array.prototype.forEach.apply(elem,[callback])方法

 

//添加点击事件
Array.prototype.forEach.apply(hdList,[(current,index) => {
	current.addEventListener('click',() => {
		animationFn(index);
	},false);
}]);

 

 

(6),用bind方法

 

 

//添加点击事件
Array.prototype.forEach.bind(hdList)((current,index) => {
	current.addEventListener('click',() => {
		animationFn(index);
	},false);
});


参考:

 

JavaScript中的apply()方法和call()方法使用介绍

http://www.jb51.net/article/30883.htm
 

1-DOM中-类数组对象遍历(转换)为数组

http://blog.youkuaiyun.com/bug_money/article/details/53148678

其他

[我的博客,欢迎交流!](http://rattenking.gitee.io/stone/index.html)

[我的优快云博客,欢迎交流!](https://blog.youkuaiyun.com/m0_38082783)

[微信小程序专栏](https://blog.youkuaiyun.com/column/details/18335.html)

[前端笔记专栏](https://blog.youkuaiyun.com/column/details/18321.html)

[微信小程序实现部分高德地图功能的DEMO下载](http://download.youkuaiyun.com/download/m0_38082783/10244082)

[微信小程序实现MUI的部分效果的DEMO下载](http://download.youkuaiyun.com/download/m0_38082783/10196944)

[微信小程序实现MUI的GIT项目地址](https://github.com/Rattenking/WXTUI-DEMO)

[微信小程序实例列表](http://blog.youkuaiyun.com/m0_38082783/article/details/78853722)

[前端笔记列表](http://blog.youkuaiyun.com/m0_38082783/article/details/79208205)

[游戏列表](http://blog.youkuaiyun.com/m0_38082783/article/details/79035621)

 

 

 

 

posted @ 2017-09-29 10:05 Newman·Li 阅读( ...) 评论( ...) 编辑 收藏
<table align="center" border="0" cellpadding="0" cellspacing="0" class="tableForm" style="table-layout: fixed;"> <colgroup> <col width="80" /> <col /><!--hide4phone.start--> <col width="80" /> <col width="380" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td style="text-align: right;"> <span style="color: rgb(255, 0, 0);">*</span>Subject:</td> <td dbf.type="required" id="dbf.subject"> </td> <!--show4phone.start></tr><tr><show4phone.end--> <td style="text-align: right;"> Status:</td> <td><span id="mapping.dbf.procXSource"> </span>      Responsor: <span id="mapping.dbf.responsorSource"> </span>      Participants: <span id="mapping.dbf.participantsSource"> </span></td> </tr> </tbody> </table> <div> </div> <div style="text-align: center;"> <h1><img src="../common/logo.png" /> [Form Name]</h1> </div> <div>[Design form here, based on the template below, or customized by yourself after the template removed]</div> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder" style="table-layout: fixed;"> <colgroup> <col width="130" /> <col /><!--hide4phone.start--> <col width="130" /> <col width="330" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td colspan="4" colspan4phone="2" dbf.source="" dbf.type="" style="background-color: lightyellow;"> <strong>[Brief Information]</strong></td> </tr> <tr> <td class="fieldLabel" style="text-align: center; width: 147px;"><span style="color: red;">*</span> username</td> <td id="username" style="width: 210px;"> </td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td class="fieldLabel" style="text-align: center; width: 63px;"><span style="color: red;">*</span> id</td> <td id="ID" style="width: 254px;"> </td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">项目名称</td> <td id="项目名称" style="width: 210px;"> </td> <td class="fieldLabel" style="text-align: center;">装置名称</td> <td id="装置名称" style="width: 254px;"> </td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">Customer Name</td> <td id="CustomerName" style="width: 210px;"> </td> <td class="fieldLabel" style="text-align: center;">E-NO/序列号</td> <td id="ENO号" style="width: 254px;"> </td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">产品描述</td> <td id="产品描述" style="width: 210px;"> </td> <td class="fieldLabel" style="text-align: center;">Remark</td> <td id="Remark" style="width: 254px;"> </td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">Subject</td> <td id="subjectid" style="width: 210px;"> </td> <td class="fieldLabel" dbf.source="" dbf.type="" id="" style="text-align: center;"> </td> <td dbf.source="" dbf.type="" id="" style="width: 254px;"> </td> </tr> <tr> <td colspan="4" colspan4phone="2" dbf.source="" dbf.type="" style="background-color: lightyellow;"><strong> </strong><strong>[Detailed Information]</strong></td> </tr> </tbody> </table> <div style="text-align: center; margin: 20px 0;">        <input id="idslist" name="idslist" type="hidden" />  <input id="sql内容" name="sql内容" type="hidden" />                                                                 <strong> <input id="check" name="check" onclick="clickData()" type="button" value="check记录" /> </strong> <strong> <input id="check2" name="check2" onclick="clickData2()" type="button" value="下载结果" /> </strong></div> <!-- ==================== 移动端表格占位(暂不使用)==================== --><!-- ==================== 查询结果表格容器 ==================== --> <div id="resultTableContainer" style="margin-top: 20px; padding: 0 10px; min-height: 20px;"><!-- 动态表格将插入到这里 --></div> <div class="slide4phone2" id="reptable" style="display:none;"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="width: 1300px;"> </table> </div> <!-- ==================== 核心脚本:查询与渲染 ==================== --><script language="javascript"> function clickData() { if (window.__isQuerying) return; window.__isQuerying = true; try { var enonumber = $("ENO号")?.value ? $("ENO号").value().trim() : ""; if (!enonumber) { alert("请输入搜索内容"); return; } console.log("【原始输入】", enonumber); function escapeSql(str) { return String(str || '').replace(/'/g, "''"); } // ✅ 正确构建模糊匹配模式 const searchPattern = '%' + escapeSql(enonumber) + '%'; // ✅ 清洗字段中的各种隐藏字符 const cleanedField = ` REPLACE( REPLACE( REPLACE( REPLACE( ISNULL([ENO序列号], ''), CHAR(10), ''), CHAR(13), ''), CHAR(9), ''), ' ', '') `; var sql = ` SELECT TOP 100 Subject, Status, 申请人, 申请日期, 产品类别, 其他, type, 内勤, 系统, customer, ProjectName, DeviceName, 新产品, [ENO序列号], 目标价, 数量, 产地, 含税报价, HandlerRemark, Remark, 描述 FROM X_BPM_DWH_819_InquiryLines WHERE ${cleanedField} LIKE '${searchPattern}' ORDER BY 申请日期 DESC `; console.log("【执行SQL】", sql); var rawResult = service("common.js", "getDbsRecords", sql, "array"); let arr = parseServiceResult(rawResult); renderTable(arr); } catch (e) { console.error("查询失败", e); alert("错误:" + (e.message || e)); } finally { window.__isQuerying = false; } } /** * 安全解析 service 返回的结果 * 支持:合法JSON / 非法JSON / JS数组字面量 / 字符串化数组 / null等 */ function parseServiceResult(rawResult) { console.log("【原始返回类型】", typeof rawResult); console.log("【原始内容】", rawResult); // 情况1:已经是数组 if (Array.isArray(rawResult)) { console.log("✅ 已识别为原生数组"); return rawResult; } // 情况2:null/undefined/false if (rawResult == null || rawResult === 'null' || rawResult === 'undefined') { console.warn("⚠️ 返回为空值"); return []; } // 转为字符串处理 let str = String(rawResult).trim(); if (!str) { console.warn("⚠️ 返回空字符串"); return []; } // 情况3:可能是HTML错误页(常见于服务异常) if (str.startsWith('<') || str.includes('<html') || str.toLowerCase().includes('error') || str.includes('Exception')) { console.error("🔴 返回了HTML错误页!", str.substring(0, 200)); throw new Error('服务端错误,请检查SQL语法或联系管理员'); } /** * 数据清洗函数:修复各种非法字符 */ function clean(str) { return str .replace(/\\\\/g, '\\') // 修复多个反斜杠 .replace(/"/g, '\\"') // 先转义双引号 .replace(/'/g, '"') // 单引号 → 双引号 .replace(/\\"/g, '"') // 还原正常引号 .replace(/\\n/g, '\\n') // 显式换行 .replace(/\\r/g, '\\r') .replace(/\\t/g, '\\t') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') .replace(/\t/g, '\\t') .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '') // 移除控制字符 .replace(/^"(\[.*\])"$/, '$1') // 去掉 "[...]" 外层引号 .replace(/^"(\{.*\})"$/, '$1') .replace(/,\s*}/g, '}') // 去除对象尾部逗号 .replace(/,\s*]/g, ']'); // 去除数组尾部逗号 } // 尝试多种解析方式 const parsers = [ // 方式1:直接JSON.parse () => JSON.parse(str), // 方式2:清洗后再解析 () => JSON.parse(clean(str)), // 方式3:作为JS表达式执行(仅限可信环境) () => new Function(`return (${str});`)(), // 方式4:进一步清理后尝试 () => JSON.parse(clean(str).replace(/""/g, '"').replace(/"\s+:/g, '":').replace(/:\s+"/g, ':"')) ]; for (let i = 0; i < parsers.length; i++) { try { const result = parsers[i](); if (Array.isArray(result)) { console.log(`✅ 第 ${i+1} 种方式解析成功`); return result; } // 兼容 { data: [...] } 结构 if (result && Array.isArray(result.data)) return result.data; if (result && Array.isArray(result.rows)) return result.rows; } catch (e) { console.warn(`📌 方式 ${i+1} 解析失败`, e.message); } } // 情况5:手动解析简单二维数组字符串 [[...],[...]] if (/^\[\s*$$[^$$]*$\s*(,\s*$$[^$$]*$)*\s*\]$/.test(str)) { try { console.log("🔍 检测到疑似二维数组字符串,尝试手动解析"); const rows = str.match(/$[^$$]*$/g) || []; return rows.map(row => { return row.replace(/^\[/, '').replace(/\]$/, '') .split(',') .map(field => field.trim().replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1')); }).filter(r => r.length > 0); } catch (e) { console.error("手动解析失败", e); } } // 所有方式都失败 console.error("🔴 所有解析方式均失败", { raw: rawResult, cleaned: clean(str) }); throw new Error('返回数据格式异常,无法解析。请检查SQL是否正确或联系开发人员。'); } /** * 渲染查询结果为表格 */ function renderTable(arr) { var container = document.getElementById("resultTableContainer"); if (!container) return; // 清空容器 container.innerHTML = ''; if (!arr || arr.length === 0) { container.innerHTML = ` <p style="text-align:center;color:#999;font-style:italic;padding:20px;"> 📭 未找到匹配的数据。 </p> `; return; } // 创建表格 var table = document.createElement('table'); table.className = 'tableListBorder'; table.style.width = '100%'; table.style.tableLayout = 'fixed'; table.setAttribute('border', '1'); table.setAttribute('cellpadding', '6'); table.setAttribute('cellspacing', '0'); table.style.fontFamily = 'Arial, sans-serif'; table.style.fontSize = '13px'; // 表头 var thead = document.createElement('thead'); var headerRow = document.createElement('tr'); var headers = [ "Subject", "Status", "申请人", "申请日期", "产品类别", "其他", "类型", "内勤", "系统", "客户", "项目名称", "装置名称", "新产品", "E-NO/序列号", "目标价", "数量", "产地", "含税报价", "处理备注", "备注", "描述" ]; headers.forEach(function(text) { var th = document.createElement('th'); th.style.backgroundColor = '#f5f5f5'; th.style.textAlign = 'center'; th.style.padding = '10px'; th.style.whiteSpace = 'nowrap'; th.style.fontWeight = 'bold'; th.textContent = text; headerRow.appendChild(th); }); thead.appendChild(headerRow); table.appendChild(thead); // 表体 var tbody = document.createElement('tbody'); arr.forEach(function(row, index) { if (!row || !Array.isArray(row) && typeof row !== 'object') { console.warn(`第 ${index} 行数据异常`, row); return; } var tr = document.createElement('tr'); Object.values(row).forEach(function(cell) { var td = document.createElement('td'); td.style.padding = '8px'; td.style.borderTop = '1px solid #eee'; td.style.wordBreak = 'break-word'; td.style.maxWidth = '200px'; td.textContent = cell == null ? '' : String(cell).trim(); tr.appendChild(td); }); tbody.appendChild(tr); }); table.appendChild(tbody); container.appendChild(table); console.log(`✅ 成功渲染 ${arr.length} 行数据`); } </script> <div id="resultTableContainer" style="margin-top: 20px; overflow-x: auto;"> </div> 完整修复以上 [ENO序列号] 匹配问题 数值不能完全匹配
最新发布
10-22
<think>首先,用户的问题是:“请问JavaScript result.forEach is not a function 错误原因”。用户遇到了一个错误,提示是“result.forEach is not a function”,想知道原因。 根据系统级指令,我需要: - 使用中文回答。 - 结构清晰,帮助用户逐步解决问题。 - 保证回答真实可靠。 - 在回答中引用的段落末尾自然地添加引用标识。 - 生成相关问题。 参考的站内引用: - [^1]: 报错信息显示,尝试在某个对象上调用 forEach 方法时出错,因为该对象不是函数。 - [^2]: 解释了 Array.forEach 的特性,它不返回任何值,并且强调了它只适用于数组- [^3]: 错误是因为尝试在一个对象上使用 forEach,而 forEach 只适用于数组- [^4]: 展示了如何将类数组对象转换为数组来使用 forEach。 这些引用表明,错误的核心原因是:尝试在一个不是数组对象上调用 forEach 方法。因为 forEach数组的方法,只能用于数组类数组对象(但需要处理)。 我的回答结构应该清晰: 1. **解释错误原因**:直接说明错误是因为 result 不是数组。 2. **详细分析**:解释为什么 forEach 只能用于数组,并提供常见场景。 3. **解决方案**:给出如何避免这个错误的方法。 4. **生成相关问题**:在最后添加相关问题。 确保使用正确的 LaTeX 格式: - 行内数学表达式用 $...$,例如 $x^2$。 - 独立公式用 $$...$$ 并单独成段。 - 但在这个问题中,可能不需要数学表达式,除非涉及计算。主要讨论 JavaScript 错误。 回答内容: - **原因**:result 变量不是一个数组,而 forEach 是 Array 原型上的方法,只能用于数组- **常见场景**: - 当从 API 响应中获取数据时,可能返回对象而不是数组- 当变量被错误地赋值为非数组值时。 - 当处理类数组对象(如 NodeList)时,需要先转换为数组- **解决方案**: - 检查 result 的类型:使用 Array.isArray(result) 或 typeof。 - 如果 result 是类数组对象,使用 Array.from(result) 或 [...result] 转换为数组- 确保数据源返回数组- **引用引用**:在适当的地方添加引用标识,如根据 [^3],当 res 是对象时调用 forEach 会报错。 生成相关问题: 1. 如何检查 JavaScript 变量是否为数组? 2. 如何将类数组对象转换为数组? 3. 除了 forEach,还有哪些数组遍历方法? 最后,确保回答流畅、自然。</think>### JavaScript `result.forEach is not a function` 错误原因分析 这个错误通常发生在 JavaScript 中尝试对非数组变量调用 `forEach` 方法时。`forEach` 是数组(Array)原型上的方法,只能用于数组类数组对象。如果 `result` 不是数组,调用 `result.forEach()` 就会抛出此错误。下面我将逐步解释原因、常见场景和解决方案。 #### 1. **错误根本原因** - `forEach` 是 JavaScript 中数组对象的内置方法,用于遍历数组元素。语法为: ```javascript array.forEach(callback(currentValue, index, array)); ``` - 如果 `result` 不是数组(例如是对象、字符串、数字或 `null`),JavaScript 引擎无法找到 `forEach` 方法,因此报错 `TypeError: result.forEach is not a function`。 - 关键点:`result` 的数据类型必须是数组才能使用 `forEach`。常见错误场景包括: - **API 响应处理**:从服务器获取的数据可能是对象(如 `{ data: [...] }`),而非直接数组[^3]。 - **变量赋值错误**:`result` 被意外赋值为非数组值(如未初始化时为 `undefined`)。 - **类数组对象未转换**:如 DOM 集合(NodeList)或函数参数(`arguments`),它们像数组但不是真正的数组,需要显式转换[^4]。 #### 2. **常见错误场景示例** - **场景1:API 返回对象而非数组** - 例如,API 响应 `res` 是一个对象,包含嵌套数据: ```javascript const res = { data: { items: [1, 2, 3] } }; // res 是对象,不是数组 res.forEach(item => console.log(item)); // 报错:res.forEach is not a function ``` 正确做法是访问嵌套数组:`res.data.items.forEach(...)`[^3]。 - **场景2:变量类型错误** - 如果 `result` 是 `null` 或 `undefined`: ```javascript let result = null; result.forEach(...); // 报错:无法调用 null 的方法 ``` - 或意外赋值为字符串: ```javascript let result = "hello"; result.forEach(...); // 报错:字符串没有 forEach 方法 ``` - **场景3:类数组对象未转换** - DOM 查询返回的 NodeList类数组对象: ```javascript const elements = document.querySelectorAll('div'); // NodeList 类型 elements.forEach(...); // 可能报错(旧浏览器中),需转换为数组 ``` 解决方案:使用 `Array.from(elements)` 或 `[...elements]`[^4]。 #### 3. **诊断和解决方案** - **检查变量类型**: - 使用 `Array.isArray()` 或 `typeof` 验证 `result` 是否为数组: ```javascript console.log(Array.isArray(result)); // true 表示是数组 console.log(typeof result); // 应为 "object"(数组是特殊对象) ``` - 如果不是数组,检查数据来源(如 API 响应结构)并修正访问路径。 - **转换类数组对象**: - 如果 `result` 是类数组(如 NodeList 或 `arguments`),转换为数组: ```javascript // 方法1: Array.from() const arrayResult = Array.from(result); arrayResult.forEach(...); // 方法2: 展开运算符 const arrayResult = [...result]; arrayResult.forEach(...); ``` - **确保数据一致性**: - 在异步操作(如 fetch API)中,确认响应数据解析正确: ```javascript fetch('url') .then(response => response.json()) .then(data => { if (Array.isArray(data)) { // 检查是否为数组 data.forEach(...); } else { console.error('Data is not an array:', data); } }); ``` - **备选遍历方法**: - 如果无法转换数组,使用 `for...of` 循环(支持类数组): ```javascript for (const item of result) { // result 可以是数组或可迭代对象 console.log(item); } ``` #### 4. **预防措施** - **代码防御**:在调用 `forEach` 前添加类型检查。 ```javascript if (Array.isArray(result)) { result.forEach(...); } else { console.error('result is not an array'); } ``` - **理解数据流**:在数据处理链中,明确每个变量的类型,避免隐式转换错误。 - **参考文档**:`forEach` 仅适用于数组,不返回任何值(无返回值),且不改变原数组[^2]。 通过以上步骤,您可以快速定位并修复此错误。如果问题持续,建议检查数据源或调试工具(如 `console.log(result)`)来验证变量类型[^1][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值