# JavaScript Make average of values inside array to smooth graph line make average values

I have an `array` which represents the points of a graph with different values like the following one:

``````var array = [5, 3, 4, 1, 2];
``````

I would like to loop through it and create a new array where the new values are:

• An average between the value preceding it and the one coming after it.
• Placed among the existing ones.

This means that `array[0]` will remain at the same position, while the other values will be pushed of one position. The new array should look like this:

``````var newArray = [5, 4, 3, 3.5, 4, 2.5, 1, 1.5, 2];
``````

Do you have an idea on how to achieve this? Thanks in advance to your replies!

``````var array = [5, 3, 4, 1, 2];
var newArr = [array[0]]; // start the array with the first from the original

array.reduce((a, b) => {
newArr.push((a + b) / 2, b);
return b;
});

console.log(newArr);``````
``````var array = [5, 3, 4, 1, 2];

var newArray = [];
newArray.push(array[0]);

for(var i=0; i < array.length-1; i++)
{
var first = array[i];
var second = array[i+1];
var avg = (first+second)/2;
newArray.push(avg);
newArray.push(second);
}
``````

https://jsfiddle.net/5utkvge8/

You are going to want to loop through your original array, pushing each number to the new one, and if you are not on the final element, get the average of `array[i]` and `array[i+1]`

``````var array = [5, 3, 4, 1, 2];
var newArray = [];

for (var i = 0; i < array.length; i++)
{
newArray.push(array[i])
if (!isNaN(array[i+1]))
{
newArray.push((array[i] + array[i+1]) / 2)
}
}
``````

or in a functional, no-side effects, way:

``````var array = [5, 3, 4, 1, 2];
var newArray = array.reduce((result, value, index, array) => result.concat(index > 0 && index < array.length ? [(array[index-1] + value)/2, value] : value), [])
``````

In case you can modify the original array:

``````var array = [5, 3, 4, 1, 2],
len = array.length * 2 - 2;

for (var i = 1; i < len; i = i + 2) {
array.splice(i, null, (array[i-1] + array[i]) / 2);
}

console.log(array);``````

## Angular Create Dynamic year dropdown

I want to create a year selection dropdown. year must start from this year upto next 7 years . I have tried var year = new Date().getFullYear(); var range = []; range.push(year); for (var i = 1; i ...

I want to create a year selection dropdown. year must start from this year upto next 7 years . I have tried var year = new Date().getFullYear(); var range = []; range.push(year); for (var i = 1; i ...

1. angular create dynamic component
2. angular create dynamic forms
3. angular create dynamic module
4. angular create dynamic table
5. angular create dynamic object
6. angular create dynamic array
7. angular create dynamic year dropdown
8. angular create dynamic id
9. angular create dynamic css class
10. angular create dynamic templateref
12. angular create dynamic class
13. angular create dynamic variable
14. angular create dynamic form group
15. angular create dynamic template variable
16. angular create dynamic variable name
17. angular create dynamic html
19. angular create dynamic template
20. angular create dynamic element

## \$timeout inside will loop creates infinite loop

I cannot seem to get the timeout and the loop to work together. If I remove the loop it works or if I remove the timeout it will work. Together I will get an infinit loop and it doesnt seem like the ...

I cannot seem to get the timeout and the loop to work together. If I remove the loop it works or if I remove the timeout it will work. Together I will get an infinit loop and it doesnt seem like the ...

## Callback for find not working-Javascript

I have written a custom callback function for Javascript's find function but that is always yielding undefined var objectsArray = [ { 'a': 1, 'b': 2, 'c': 3 }, { 'a': 41, 'b': 5, 'c': 7 }, ...

I have written a custom callback function for Javascript's find function but that is always yielding undefined var objectsArray = [ { 'a': 1, 'b': 2, 'c': 3 }, { 'a': 41, 'b': 5, 'c': 7 }, ...

## How to make a parallel lines drawing

I have this [sample][1], what I want is to have this result[![enter image description here][2]][2] Note: a.) 1 and 2 will be connected while 3 will be produced in the third mousedown click. b.) 1,...

I have this [sample][1], what I want is to have this result[![enter image description here][2]][2] Note: a.) 1 and 2 will be connected while 3 will be produced in the third mousedown click. b.) 1,...

1. make parallel lines in illustrator
2. make parallel lines photoshop
3. make parallel lines in autocad
4. make parallel lines
5. make parallel lines adobe illustrator
6. make parallel lines in sketchup
7. make lines parallel solidworks
8. make lines parallel powerpoint
9. inkscape make parallel lines
10. make parallel curved lines
11. how to make parallel lines with a compass
12. how to make parallel lines using compass
13. how to make parallel lines in construction
14. how to make parallel lines with set squares
15. how to make parallel lines on geogebra
16. how to make parallel lines geometry
17. how to make parallel lines on geometer's sketchpad
18. make two lines parallel illustrator
19. make two lines parallel in autocad
20. how to make parallel lines on keyboard
1