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);
Related
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 3 months ago.
Improve this question
const arrFilter = [{id:1},[],[]];
how can I filter the empty array in the above example, so that the result would be the object with an id only
You can use the length of the array returned from Object.keys() to determine if objects, arrays or strings are empty however it won't work on numbers.
With that in mind, try this assuming that
all empty objects, arrays and strings should be omitted
everything else stays
const arrFilter = [{id:1},[],[], "a string", "", 1, 0];
const nonEmpties = arrFilter.filter(
(item) => typeof item === "number" || Object.keys(item).length > 0
);
console.log(nonEmpties);
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 8 months ago.
Improve this question
My array
const array = [2,3];
and I want object like
"media" :[{"mid":"2"},{"mid":"3"}]
Thanks in advance.
You can use Array.prototype.map to create the desired array and then you can create an object and assign the array to the media key.
const array = [2, 3];
const obj = { media: array.map((item) => ({ mid: item })) };
console.log(obj);
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)
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 2 years ago.
Improve this question
What I'm doing I'm trying to remove the quotation in array in Java
this is the code
Array [ "johan|39012|manager|2010", "" ]
What I wanna or what I aspect
"johan|39012|manager|2010"
Try this and you will have an array with of good string:
const arr = [ "johan|39012|manager|2010", "" ];
const newArr = arr.filter(item => item.length > 0);
console.log(newArr);
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);