How to Find Middle Element of Array in JavaScript

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 and arr.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.