0

How can I remove the same value queryString when searching for ID in jQuery, ID is being pass in textarea and calls an API when searching. When searching for similar IDs it shows two similar items, so if I can remove the same queryString values it would prevent it from fetching similar items.

QueryString looks like this Product?id=1&id=1 then renders the item twice.

Getting the values from 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 + "&";
}
4
  • What is inside the textarea ? Commented Feb 14, 2017 at 10:53
  • Id numbers (e.g id=1) Commented Feb 14, 2017 at 10:53
  • Thus user can write "id=12id=48waow" ? Commented Feb 14, 2017 at 10:54
  • @Weedoze they can but that won't show anything but an error, the question is only concern about duplicate values like id=1&id=1 Commented Feb 14, 2017 at 10:57

3 Answers 3

1

You can extend your use of array.prototype.filter() to achieve uniqueness before joining:

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 + "&";
}
Sign up to request clarification or add additional context in comments.

8 Comments

It's not removing duplicate values
Of course you'll have to use the unique values for joining, not the original - fixed that in the answer. There's also a more elegant way that makes use of array.filter() instead, I will add that to the answer.
Updated the code in the answer to extend the filtering before the join.
the above answer to extend filter, self.indexOf() is not a function it says.
jsperf.com/filter-vs-helperarray/1 - in Chrome, the difference is not that huge for me, but in Firefox, filter() is about three times faster. Definitely go for using filter() - I'm going to remove the original approach from my answer.
|
0

instead of concatenating some values you can use something like map collection in javascript

  • map is a data structure that every key matching to specific value and if two times for a specific key, any value is set previous value always will be overridden

for map collection implementation in js take a look on following link

http://www.collectionsjs.com/map

Comments

0

How about this ? It uses some data structures that are new and not fully compatible. But it works

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.