作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试查找 JSON 字符串中的所有“a”键并将它们保存在数组中。由于它们可能嵌套在不同的级别,因此我使用递归来遍历每个键的子级,以详尽地列出所有“a”键。但是,我无法专注于递归子 Node 并将任何找到的“a”键附加到返回的数组。如有任何帮助,我们将不胜感激。
遍历JSON字符串的函数:
searchForKeys = (keyValue, obj) => {
let arrayOfKeys = [];
if (obj instanceof Array) {
obj.forEach((item, index) => {
if(typeof(item) === 'object' && Object.keys(item)[index] === keyValue) {
console.log('pushing found item to return array');
arrayOfKeys.push(...item);
console.log(arrayOfKeys);
}
});
}
else if (typeof(obj) === 'object') {
if (Object.keys(obj).length > 0) {
let itemName;
for(let i = 0; i < Object.keys(obj).length; i++) {
itemName = Object.keys(obj)[i];
if (itemName === keyValue) {
console.log('pushing found item to return array');
arrayOfKeys.push(obj[itemName]);
}
}
console.log('arrayOfKeys.length:', arrayOfKeys.length);
arrayOfKeys.push(searchForKeys(keyValue, obj[itemName]));
}
return arrayOfKeys;
}
};
let returnedArray = searchForKeys("a", dummyJSON);
console.log("array content:\n", [...returnedArray]);
console.log("array length", returnedArray.length);
预期结果是返回数组,其中包含 JSON 对象中的所有四个“a”键。
源数据:
const dummyJSON = {
"a": {
"@attributes": {
"href": "/URL/2021.html?q=link+value",
"title": "Lautsprecher",
"class": "catfilter-list-link",
"data-gtm-event": "filter.click",
"data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
"data-metric-click": "mainsearch-catfilter-click"
},
"#text": "Lautsprecher"
},
"ul": {
"@attributes": {
"class": "catfilter-list"
},
"#text": [],
"li": [
{
"@attributes": {
"class": "catfilter-list-item"
},
"#text": [
"",
""
],
"a": {
"@attributes": {
"href": "/URL/8412.html?q=link+value",
"title": "Handy-Ladegeräte",
"class": "catfilter-list-link",
"data-gtm-event": "filter.click",
"data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
"data-metric-click": "mainsearch-catfilter-click"
},
"#text": "Handy-Ladegeräte"
}
},
{
"@attributes": {
"class": "catfilter-list-item"
},
"#text": [
"",
""
],
"a": {
"@attributes": {
"href": "/URL/26136.html?q=link+value",
"title": "Kabellose Ladegeräte",
"class": "catfilter-list-link",
"data-gtm-event": "filter.click",
"data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
"data-metric-click": "mainsearch-catfilter-click"
},
"#text": "Kabellose Ladegeräte"
}
},
{
"@attributes": {
"class": "catfilter-list-item"
},
"a": {
"@attributes": {
"href": "/URL/2021.html?q=link+value",
"title": "Lautsprecher",
"class": "catfilter-list-link",
"data-gtm-event": "filter.click",
"data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
"data-metric-click": "mainsearch-catfilter-click"
},
"#text": "Lautsprecher"
},
"#text": [
"",
""
],
"a": {
"@attributes": {
"href": "/URL/2021.html?q=link+value",
"title": "Lautsprecher",
"class": "catfilter-list-link",
"data-gtm-event": "filter.click",
"data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
"data-metric-click": "mainsearch-catfilter-click"
},
"#text": "Lautsprecher"
}
}
]
}
};
最佳答案
对于 JSON 字符串,JSON.parse
reviver parameter可以使用:
var arr = [], json = JSON.stringify({"a":{"@attributes":{"href":"/URL/2021.html?q=link+value","title":"Lautsprecher","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Lautsprecher"},"ul":{"@attributes":{"class":"catfilter-list"},"#text":[],"li":[{"@attributes":{"class":"catfilter-list-item"},"#text":["",""],"a":{"@attributes":{"href":"/URL/8412.html?q=link+value","title":"Handy-Ladegeräte","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Handy-Ladegeräte"}},{"@attributes":{"class":"catfilter-list-item"},"#text":["",""],"a":{"@attributes":{"href":"/URL/26136.html?q=link+value","title":"Kabellose Ladegeräte","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Kabellose Ladegeräte"}},{"@attributes":{"class":"catfilter-list-item"},"a":{"@attributes":{"href":"/URL/2021.html?q=link+value","title":"Lautsprecher","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Lautsprecher"},"#text":["",""],"a":{"@attributes":{"href":"/URL/2021.html?q=link+value","title":"Lautsprecher","class":"catfilter-list-link","data-gtm-event":"filter.click","data-gtm-payload":"{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}","data-metric-click":"mainsearch-catfilter-click"},"#text":"Lautsprecher"}}]}})
JSON.parse(json, (key, val) => key == 'a' ? arr.push(val) : val)
console.log( arr )
关于javascript - 从 JSON 字符串中提取嵌套的 <a> 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56827823/
我是一名优秀的程序员,十分优秀!