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]
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 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]
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 two arrays. For example:
var array1 = ["Q3","Q4","Q5","Q6","Q7","Q8"];
var array2 = ["Q5","Q6","Q7"];
The output of second array should be,
[0,0,"Q5","Q6","Q7",0];
I want to compare the first array with the second and fill the missing values with Zero 0.
array1 is the primary array and, for now, array2 will have this 3 values initially.
Given the example arrays a simple map -> includes would work:
var array1 = ["Q3","Q4","Q5","Q6","Q7","Q8"];
var array2 = ["Q5","Q6","Q7"];
console.log(array1.map(i => array2.includes(i) ? i : 0))
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
[1,2,3,4,5,6] should be changed to set containing {1,2},{2,3},{3,4},{4,5},{5,6} using normal for loop and using javascript map and reduce functions convert array to set of pairs using different techniques Thanks in Advance
Why not a simple for loop? Array functions are great but are often unnecessary.
const arr = [1,2,3,4,5,6]
const result = []
for (let i = 1; i < arr.length; ++i)
result.push([arr[i-1], arr[i]])
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 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.