gpt4 book ai didi

JavaScript - 如何根据唯一的 # 从数组中获取唯一的 url?

转载 作者:太空宇宙 更新时间:2023-11-04 16:20:14 26 4
gpt4 key购买 nike

我正在尝试从此数组中获取唯一的 URL。

var urls = [
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
]

从代码来看,第五个数组是['791','246','796','051','056'],所以输出是这样的:

[
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
]

从另一个答案中,我能够获取数组中唯一的第五个代码:

var unique = ['791','246','796', '051','056']

然后我尝试执行此操作以获取唯一的 URL 并将其推送到空数组,但没有成功。

var uniqueUrls = []
for(i=0; i < unique.length; i++){
for(j=0; j< urls.length; j++){
if(urls[j].indexOf(unique[i])){
uniqueUrls.push(urls[j]);
break;
}
}
}

有人可以帮我解决这个问题吗?我在 for 循环中做错了什么吗?或者是否有更好的方法从 URL 数组中获取唯一的 URL,而不使用双 for 循环?

更新

忘记提及其他数字代码并不总是“零”,它们可以是任何随机数,但我仍然有兴趣根据第五个代码获取唯一的网址。

最佳答案

您可以使用 ES6 Set 和数组扩展来获取唯一值:

const getUnique = (urls) => [...new Set(urls.map((url) => url.match(/\/banner\?l_id=(?:\w+-){4}(\w+)/)[1]))];

var urls = [
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
];

console.log(getUnique(urls));

如果您无法使用 ES6,请使用带有哈希对象的reduce来检查唯一性:

function getUnique(urls) {
var hash = {};

return urls.reduce(function(arr, url) {
var id = url.match(/\/banner\?l_id=(?:\w+-){4}(\w+)/)[1];

if(!hash[id]) {
arr.push(id);

hash[id] = true;
}

return arr;
}, []);
}

var urls = [
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-791-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-246-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-796-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-051-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
"http://display.companyX.com/rest/banner?l_id=A000-0000-0000000-000000-056-0-0-0&k=A77K_fakfhaskfhkjshfkjo=kafkjafk",
];

console.log(getUnique(urls));

关于JavaScript - 如何根据唯一的 # 从数组中获取唯一的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40709396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com