Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a search query like
`const query = '?sortBy=recent&page=2&comments=true&sortBy=rating' // two repeating params 'sortBy'
How can I use regex for checking is there any repeating params ????
Not recommended to use regex.
Try this
const query = new URLSearchParams('?sortBy=recent&page=2&comments=true&sortBy=rating');
const keys = [...query.keys()]; // convert iterable to array
console.log(keys)
const unique = keys.length === new Set(keys).size; // return false if dupes found
console.log(unique);
// to get the dupe(s)
const dupes = keys.filter((e, i, a) => a.indexOf(e) !== i)
console.log(dupes)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
i have this array:
var arr = ["apple","potato","carrot","tomato","grape","banana","potato","carrot","carrot"];
As you can see, we have "potato" 2 times in this array, and also "carrot" 3 times in this array. How can i create a new array, that will content only "potato" and "carrot" values, so only values that are not single/unique in initial array?
Like this:
["potato","carrot"];
How it can be done in js?
Check this one liner:
[...new Set(arr.filter(e => arr.filter(a => a === e).length > 1))];
arr.filter(e ...) - filter array for duplicates
[...new Set(...))] - create array with unique values using set and spread syntax
Working snippet:
var arr = ["apple","potato","carrot","tomato","grape","banana","potato","carrot","carrot"];
console.log([...new Set(arr.filter(e => arr.filter(a => a === e).length > 1))]);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am working with an array in Javascript that contains several IDs in them but I would like to filter out all non-numeric entries using regex and return that array of just numbers. For example, I have myArray = ['131125150138677','CI%20UW%20SYSTEMS%20S','040964100010832'] where I want to get rid of the second item in the list since it's non-numeric.
So use Filter and test to see if they are numbers
var myArray = ['131125150138677', 'CI%20UW%20SYSTEMS%20S', '040964100010832']
var filtered = myArray.filter(Number)
console.log(filtered)
var filtered2 = myArray.filter(s => s.match(/^\d+$/))
console.log(filtered2)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
My Initial array:
var mainArray = ['car','incl','arc','linc','rca','icnl','meta','tame'];
I want the result like:
[['car','arc','rca'],['linc','icnl','incl'],['meta','tame']];
This is a compact version by using a closure for the sorted character array.
var array = ['car', 'incl', 'arc', 'linc', 'rca', 'icnl', 'meta', 'tame'],
result = Object.values(
array.reduce(
(r, s) => (a => ((r[a] = r[a] || []).push(s), r))([...s].sort()),
{}
)
);
console.log(result);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So how we can make a function that will take arrays of string like const numbers = ['1','2','0','3','0']
and change it for const changedNumbers = ['1','1','2','2','0','3','3','0']
Iterate with Array.map(). If '0' return '0', else return an array with two current string. Flatten by spreading into Array.concat():
const numbers = ['1','2','0','3','0']
const result = [].concat(...numbers.map((s) => +s ? [s, s] : s));
console.log(result);