Split arrays into multiple arrays based on string characters [closed] - javascript

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

Related

How to convert below array data to object with property (json) in javascript or es6? [closed]

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

finding repeating params in query using regex [closed]

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)

Remove specific value inside string [closed]

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

Horizontal concatenation of two array [closed]

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
how to do horizontal concatenation of two arrays in javascript?
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
I want output like :
c = [[1,2,7], [3,4,8], [5,6,9]]
You can use map and use index to access corresponding value from second array
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
let final = a.map((v,i)=> v.concat(b[i]))
console.log(final)

how to copy previous value in array (string) [closed]

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

Categories

Resources