我正在从父节点遍历 childNodes。我想访问子节点的 tagName,但错误是类型 ChildNode 上不存在 tagName。
const contentParsed = new DOMParser().parseFromString(content, 'text/xml');
const svgBody = contentParsed.querySelector('svg');
if (svgBody === null) throw new Error('content must be svg');
svgBody.childNodes.forEach((node) => node.tagName) // Property 'tagName' does not exist on type 'ChildNode'.ts(2339)
Run Code Online (Sandbox Code Playgroud)
它适用于 javascript
const svgStr = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><title></title><path d="M 2.6846088,10.906645 H 28.979394 v 8.389403 H 15.580319 l -0.719092,1.438183 -3.811186,0.143819 -0.575273,-1.582002 H 2.6846088 Z" fill="#fff"/><path d="M2,10.555H30v9.335H16v1.556H9.778V19.889H2Zm1.556,7.779H6.667V13.666H8.222v4.667H9.778V12.111H3.556Zm7.778-6.223v7.779h3.111V18.334h3.111V12.111Zm3.111,1.556H16v3.112H14.444Zm4.667-1.556v6.223h3.111V13.666h1.556v4.667h1.556V13.666h1.556v4.667h1.556V12.111Z" fill="#cb3837"/></svg>`
const contentParsed = new DOMParser().parseFromString(svgStr, 'text/xml');
const svgBody = contentParsed.querySelector('svg');
if (svgBody === …Run Code Online (Sandbox Code Playgroud)对稀疏数组进行排序时[5, 2, 4, , 1].sort() -> [1, 2, 4, 5, empty],无论return语句如何,即使有回调,空值也始终是最后一个。
我正在构建自己的排序方法来应对挑战,由于过滤器跳过了空值,因此我使用过滤器方法解决了此问题。然后遍历过滤后的数组并将原始数组的索引设置为过滤后的数组的值。然后,我缩短了原始数组的长度,因为剩余的项将是重复项,最后可以将其输入我的排序算法中。完成此操作后,我将其长度设置回原始长度,并在末尾添加适当数量的空项目。这是一段代码,但这是整个代码的链接
const collectUndefined = [];
// remove empty items and collect undefined
const removeSparse = this.filter(el => {
if (el === undefined) {
collectUndefined.push(el);
}
return el !== undefined;
});
const tempLength = this.length;
// reset values but will contain duplicates at the end
for (let i = 0; i < removeSparse.length; i++) {
this[i] = removeSparse[i];
}
// shorten length which will remove extra duplicates …Run Code Online (Sandbox Code Playgroud)