Javascript: Finding the most middle value in an array
If you have an array with for example five items, the middle item is at index two:
var arr = [ item, item, middle, item, item];
Dividing the length by two and using Math.round
would give you index three rather than two, so you would need to subtract one from the length first:
var middle = arr[Math.round((arr.length - 1) / 2)];
You say in your question that you are supposed to use Math.round
, but if that is not a requirement, you can get the same result easier using Math.floor
:
var middle = arr[Math.floor(arr.length / 2)];
For an array with an even number of items, that will give you the second of the two items that are in the middle. If you want the first instead, use Math.floor
and substract one from the length. That still gives the same result for odd number of items:
var middle = arr[Math.floor((arr.length - 1) / 2)];
how to find middle element of array in javascript?
You can do it using recursion:
var array = [1,2,8,4,5];
function middle(i) {
if (array[i] !== undefined) {
return middle(i+1);
} else {
return array[Math.floor(i / 2)];
}
}
console.log(middle(0));
Get the middle two items of an array with even number of items
The logic is pretty easy:
- If array is empty, don't return anything
- If array has odd number of items, the index of the middle item is simply
Math.floor(arr.length / 2)
- If array has even number of items, the index of the two middle items are
arr.length / 2 - 1
andarr.length / 2
respectively
See proof-of-concept example:
function middleItem(arr) {
if (!arr.length)
return;
const mid = arr.length / 2;
if (arr.length % 2 === 1) {
return arr[Math.floor(mid)];
} else {
return [
arr[mid - 1],
arr[mid],
];
}
}
console.log(middleItem([])); // undefined
console.log(middleItem([0])); // 0
console.log(middleItem([0,1])); // [0, 1]
console.log(middleItem([0,1,2])); // 1
console.log(middleItem([0,1,2,3])); // [1, 2]
console.log(middleItem([0,1,2,3,4])); // 2
finding Middle element of an array in javascript
This should get you somewhere (from this SO answer):
if (nums.length %2 == 0) {
// even-length array (two middle elements)
var avg = (nums[(nums.length/2) - 1] + nums[nums.length/2]) /2;
}
How to find the middle value in an array containing strings
This may be what you are looking for.
Just divide the length of your array by two and round it down (Math.floor()
).
function middle(arr) { if (arr.length === 0) return undefined; return arr[Math.floor(arr.length / 2)];}
var someValues = ["abc", "def", "ghi", "jkl", "mno"];console.log(middle(someValues));
Is there alternative to get middle element from array using Ramda?
You can create midElement
by chaining R.nth and lengthDiv2
because according to R.chain documentation (and @ScottSauyet):
If second argument is a function, chain(f, g)(x) is equivalent to
f(g(x), x).
In this case g
is lengthDiv2
, f
is R.nth, and x
is the array. So, the result would be R.nth(lengthDiv2(array), array)
, which will return the middle item.
const { compose, flip, divide, length, chain, nth } = R;
const div2 = flip(divide)(2); // create the function using flipconst lengthDiv2 = compose(Math.floor, div2, length);const midElement = chain(nth, lengthDiv2); // chain R.nth and lengthDiv2
console.log(midElement([1, 5, 4])); //5
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
Related Topics
Jquery Validate Phone Number With With Regex
Pass Input Tag Value to as Href Parameter
Multiple Key Names, Same Pair Value
Show/Hide Select Options Based on Value of Another Select Using Javascript/Jquery
Save Image File from Image Url JavaScript HTML
Pass Data from HTML Table to Modal and Then to PHP Script
Correct Way to Push into State Array
Ag-Grid Cellrender With Button Click
How to Pass the Checked and Unchecked Value of Checkboxes in Component.Ts File in Angular
How to Find Middle Element of Array in JavaScript
Convert JavaScript Date Format to Yyyy-Mm-Ddthh:Mm:Ss
Safari Browser Audio.Play() Is Not Working
How to Display an Image from an Array of Images in React
How to Set a Header Field on Post a Form
Get the Contents of a Table Row With a Button Click
Angular2 Do Actions After Dom Has Finished Rendering
How to Pass Array Variable from View to Controller in Spring MVC
How to Append Value to an Array Only If Not Exists and Remove It If Not