我正在向数据库发出请求,并构建一个数组,数组的键中有 HTML 标签;我不知道如何删除 HTML 但仍保留键及其值。
我尝试过匹配正则表达式和切片,这在大多数情况下都有效,但在重建时,值会不匹配并放入错误的值持有者中。
** 编辑:这是用于从 href 标签获取名称的正则表达式。 注意:原始数组存储在tableDate中。
let newnames = []
Object.keys(tableData).forEach(function (key) {
let byeHTML = key.match(/\>(.*?)</g)[0]
noHMTLnames = byeHTML.slice(1, byeHTML.length -1)
newtab.push(noHMTLnames)
nametest = JSON.stringify(newtab)
})
console.log(nametest)
这是我正在使用的数组
[ '<a href = "http://www.torn.com/profiles.php?XID=2181524">Rum</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 11, Xanax: 0 },
'<a href = "http://www.torn.com/profiles.php?XID=2272881">cliffyd</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 },
'<a href = "http://www.torn.com/profiles.php?XID=2172188">BitMappedIn</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 4, Xanax: 0 },
'<a href = "http://www.torn.com/profiles.php?XID=767572">pilsner</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 3, Xanax: 0 },
'<a href = "http://www.torn.com/profiles.php?XID=2179037">Bantor</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 10, Xanax: 0 },
'<a href = "http://www.torn.com/profiles.php?XID=536670">chef</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 },
'<a href = "http://www.torn.com/profiles.php?XID=2283920">Yakob</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 } ]
这就是我想看到的:
[ 'Rum': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 11, Xanax: 0 },
'cliffyd': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 },
'BitMappedIn': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 4, Xanax: 0 },
'pilsner': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 3, Xanax: 0 },
'Bantor': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 10, Xanax: 0 },
'chef': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 },
'Yakob': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 } ]
最佳答案
给定的输入和输出无效。它应该是对象。您可以使用Object.fromEntries()
来更改对象的键。在 Object.entries()
上使用 map()
并更改 key 。
const obj = {'<a href = "http://www.torn.com/profiles.php?XID=2181524">Rum</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 11, Xanax: 0 }, '<a href = "http://www.torn.com/profiles.php?XID=2272881">cliffyd</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 }, '<a href = "http://www.torn.com/profiles.php?XID=2172188">BitMappedIn</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 4, Xanax: 0 }, '<a href = "http://www.torn.com/profiles.php?XID=767572">pilsner</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 3, Xanax: 0 }, '<a href = "http://www.torn.com/profiles.php?XID=2179037">Bantor</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 10, Xanax: 0 }, '<a href = "http://www.torn.com/profiles.php?XID=536670">chef</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 }, '<a href = "http://www.torn.com/profiles.php?XID=2283920">Yakob</a>': { Refill: 0, SFAK: 0, FAK: 0, BloodBag: 0, Morphine: 0, Xanax: 0 } }
const matchKey = (str) => str.match(/>=?(.+)?</g)[0].slice(1,-1);
const res = Object.fromEntries(Object.entries(obj).map(([k,v]) => [matchKey(k),v]));
console.log(res)
关于javascript - 可以从数组中的键中删除 HTML 标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55947127/