[英]remove the same value in queryString in jQuery
我如何在jQuery中搜索ID
时删除相同的值queryString, ID
在textarea
中传递,并且在搜索时调用API。 当搜索相似的IDs
它会显示两个相似的项目,因此,如果我可以删除相同的queryString值,它将阻止它获取相似的项目。
QueryString看起来像此Product?id=1&id=1
然后将该项目呈现两次。
从textarea
获取值
var getVal = $('textarea.input_' + inputSearch.name).val();
if (getVal != null && (getVal != "")) {
let inputValues = getVal
.split("\n") // allows multiple search using new line
.filter(function (str) { return str !== "" })
.join("&" + inputSearch.name + "=");
// then add it to the overall query string for all searches
query = query + inputSearch.name + "=" + inputValues + "&";
}
可以在加入之前扩展对array.prototype.filter()
以实现唯一性:
var getVal = $('textarea.input_' + inputSearch.name).val();
if (getVal != null && (getVal != "")) {
let inputValues = getVal
.split("\n") // allows multiple search using new line
.filter(function (elem, index, self) {
return elem !== "" && index == self.indexOf(elem)
})
.join("&" + inputSearch.name + "=");
// then add it to the overall query string for all searches
query = query + inputSearch.name + "=" + inputValues + "&";
}
而不是串联一些值,您可以在javascript中使用诸如map collection之类的东西
js中地图收集实现的示例,请查看以下链接
这个怎么样 ? 它使用一些新的数据结构,并且不完全兼容。 但是有效
var query='';
var parameters = new Set();
var getVal = "1\n2\n3";
if (getVal != null && (getVal != "")) {
getVal.split("\n")
.filter(function (str) { return str !== "" })
.map(function(str) {parameters.add(str)});
let query = new URLSearchParams();
for(let q of params) {
query.append(inputSearch.name,q);
}
query.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.