无法过滤类似数组的对象
Unable to filter array-like object
我想检索 HTML 中的 [image]
标签,因此我使用 DOMParser
将字符串解析为对象。但是,当使用 Lodash 的 ._filter
时,我无法获取图像数组。
奇怪的是我在使用 ._find
时能够得到结果。
我已经在网上搜索了解决方案,但找不到与我的情况相关的内容,请赐教。谢谢。
const content = '<!DOCTYPE html><html><head><meta name="keywords" content="seo, search engine optimisation, search engine optimization, search engine ranking"><meta name="description" content="A pages description, usually one or two sentences."/><title>Page title 123</title><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"></head><body><h1>Hello there</h1><p><img src="https://fallbacks.carbonads.com/nosvn/fallbacks/731050e6bd3fc6979e1cb1a972294dae.png" class="fr-fic fr-dii"></p><a href="domain.com">This is domain</a><a href="domain.com2">This is domain2</a></body></html>';
let parser = new DOMParser(),
parsedHtml = parser.parseFromString(content, 'text/html'),
tagsInHtml = parsedHtml.all;
// ## Filter the data
let hrefArray = _.filter(tagsInHtml, { "tagName": "A" }), // ??? Empty [] array
hrefArray2 = _.find(tagsInHtml, { "tagName": "A" }), // This working;
附上JSFiddle
您需要先将标签集合转换为 Array
:
const content = '<!DOCTYPE html><html><head><meta name="keywords" content="seo, search engine optimisation, search engine optimization, search engine ranking"><meta name="description" content="A pages description, usually one or two sentences."/><title>Page title 123</title><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"></head><body><h1>Hello there</h1><p><img src="https://fallbacks.carbonads.com/nosvn/fallbacks/731050e6bd3fc6979e1cb1a972294dae.png" class="fr-fic fr-dii"></p><a href="domain.com">This is domain</a><a href="domain.com2">This is domain2</a></body></html>';
let parser = new DOMParser(),
parsedHtml = parser.parseFromString(content, 'text/html'),
tagsInHtml = parsedHtml.all,
tags = Array.from(tagsInHtml);
// Filter the data
let hrefArray = _.filter(tags, { 'tagName': 'A' }),
hrefArray2 = _.find(tags, { 'tagName': 'A' });
console.dir(hrefArray);
console.dir(hrefArray2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
我想检索 HTML 中的 [image]
标签,因此我使用 DOMParser
将字符串解析为对象。但是,当使用 Lodash 的 ._filter
时,我无法获取图像数组。
奇怪的是我在使用 ._find
时能够得到结果。
我已经在网上搜索了解决方案,但找不到与我的情况相关的内容,请赐教。谢谢。
const content = '<!DOCTYPE html><html><head><meta name="keywords" content="seo, search engine optimisation, search engine optimization, search engine ranking"><meta name="description" content="A pages description, usually one or two sentences."/><title>Page title 123</title><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"></head><body><h1>Hello there</h1><p><img src="https://fallbacks.carbonads.com/nosvn/fallbacks/731050e6bd3fc6979e1cb1a972294dae.png" class="fr-fic fr-dii"></p><a href="domain.com">This is domain</a><a href="domain.com2">This is domain2</a></body></html>';
let parser = new DOMParser(),
parsedHtml = parser.parseFromString(content, 'text/html'),
tagsInHtml = parsedHtml.all;
// ## Filter the data
let hrefArray = _.filter(tagsInHtml, { "tagName": "A" }), // ??? Empty [] array
hrefArray2 = _.find(tagsInHtml, { "tagName": "A" }), // This working;
附上JSFiddle
您需要先将标签集合转换为 Array
:
const content = '<!DOCTYPE html><html><head><meta name="keywords" content="seo, search engine optimisation, search engine optimization, search engine ranking"><meta name="description" content="A pages description, usually one or two sentences."/><title>Page title 123</title><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css"></head><body><h1>Hello there</h1><p><img src="https://fallbacks.carbonads.com/nosvn/fallbacks/731050e6bd3fc6979e1cb1a972294dae.png" class="fr-fic fr-dii"></p><a href="domain.com">This is domain</a><a href="domain.com2">This is domain2</a></body></html>';
let parser = new DOMParser(),
parsedHtml = parser.parseFromString(content, 'text/html'),
tagsInHtml = parsedHtml.all,
tags = Array.from(tagsInHtml);
// Filter the data
let hrefArray = _.filter(tags, { 'tagName': 'A' }),
hrefArray2 = _.find(tags, { 'tagName': 'A' });
console.dir(hrefArray);
console.dir(hrefArray2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>