Can map function in JS do multiple transformations at once? [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 last year.
Improve this question
I wonder whether a map-function in JavaScript can do more than one transformation to an array at once.
For instance, can the items of an array be transformed to be doubled AND then to be divided by 3?
Thank you

If you want to make multiple transformations to an array element at once all you have to do is use parenthesis to separate and have them be done in a certain order.
For your example, you could do it like this:
var array = [1, 2, 3, 4];
array.map(num => (num * 2) / 3);
Which would give you
[0.6666666666666666, 1.3333333333333333, 2, 2.6666666666666665]

Related

Get the splice parameter that converts array A to array B [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 6 days ago.
Improve this question
I want to write a method to turn array A into array B by a single splice operation, returning the format [index, deleteCount, . . items], which can be used to add or delete elements, but I need to make sure that the minimum number of elements is reached in each operation.
For example, if array A is [1, 2, 2, 3] and array B is [1, 2, 5, 3], then the return result is [2,1,5].

JavaScript : How create proportionality rule function with array parameter [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 want to create an Array from another by applying the proportionality rule.
For example:
Array1 = [15,550,76,3,400,230]
I take the maximum value from Array1 which is equal in my array 550
Now, with a function, I want to create a second array Array2 with a value equal to 10 which corresponds to 550 in the first array, like Array2 = [?,10,?,?,?,?]
I want to apply the proportionality rule function to determine the other values of Array2 to get Array2=[0.27,10,1.38,0.05,7.27,4.6]
Any help please how can I create the function with array param?
Thank you.
const array1 = [15,550,76,3,400,230]
function proportionalityRule(array){
const maxValue = Math.max(...array)
return array.map(element => (element/maxValue) * 10)
}
proportionalityRule(array1)
// (6) [0.2727272727272727, 10, 1.3818181818181818, 0.05454545454545455, 7.272727272727273, 4.181818181818182]

Remove non-numeric items in a listu 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 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)

Javascript convert [[ ]] to [] [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 3 years ago.
Improve this question
Convert
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']]
to
var a= ['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']
I dont want two square brackets in front and end.
I want output
a[2] = 'a123'
The best way to do is just assigning the first value to the array:
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']];
a = a[0];
console.log(a);
But the right way of dealing it should be making sure that the endpoint or whatever that outputs should output correctly.
You can use Array.prototype.flat().
console.log([['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']].flat());
This solution works if it's extremely nested too:
console.log([
['12ae11ee12-1bhb222'],
['2019-10-10T19:46.19.632z', 'a123']
].flat());

Calculating array in javascript [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 6 years ago.
Improve this question
sorry i new learning about javascript:
i have code like this:
function call(arr) {
//i have no idea
}
console.log(call([1,2,3]));
i want to output:
//[6,3,2]
where result output obtained from [(2*3),(1*3),(1*2)]
It looks like you want each value in the array to be replaced with the value of all of the other elements multiplied together. You can calculate this more efficiently by first multiplying all of the numbers together, and then using division to take away just one number from the product at any one time, leaving the product of all the other numbers in the array.
Array.prototype.reduce
Array.prototype.map
function call(arr) {
var product = arr.reduce((a,b) => a * b, 1);
return arr.map(num => product / num);
}
console.log(call([1, 2, 3]));
Note that this won't work if a 0 appears anywhere in the array.

Categories

Resources