
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Insert value in the middle of every value inside array JavaScript
We have an array of numbers like this −
const numbers = [1, 6, 7, 8, 3, 98];
We have to convert this array of numbers into an array of objects with each object having a key as “value” and its value as a specific value of the array element. Besides this we have to insert object between two pre-existing elements with key as “operation” and using alternatively one of +, - * , / as its value.
Therefore, for the numbers array, the output would look something like this −
[ { "value": 1 }, { "operation": "+" }, { "value": 6 }, { "operation": "-"}, { "value": 7 }, { "operation": "*" }, { "value": 8 }, { "operation":"/" }, { "value": 3 }, { "operation": "+" }, {"value": 98} ]
Therefore, let’s write the code for this function −
Example
const numbers = [1, 6, 7, 8, 3, 98, 3, 54, 32]; const insertOperation = (arr) => { const legend = '+-*/'; return arr.reduce((acc, val, ind, array) => { acc.push({ "value": val }); if(ind < array.length-1){ acc.push({ "operation": legend[ind % 4] }); }; return acc; }, []); }; console.log(insertOperation(numbers));
Output
The output in the console will be −
[ { value: 1 }, { operation: '+' }, { value: 6 }, { operation: '-' }, { value: 7 }, { operation: '*' }, { value: 8 }, { operation: '/' }, { value: 3 }, { operation: '+' }, { value: 98 }, { operation: '-' }, { value: 3 }, { operation: '*' }, { value: 54 }, { operation: '/' }, { value: 32 } ]
- Related Articles
- MongoDB Increment value inside nested array?
- How to find inside an array of objects the object that holds the highest value in JavaScript?
- Increment MongoDB value inside a nested array
- Divide a scalar value into every element of a masked Array and return the floor value in NumPy
- Divide every element of a masked Array into a scalar value and return the floor value in NumPy
- Find closest value for every element in array in C++
- Find the closest value of an array in JavaScript
- Reverse index value sum of array in JavaScript
- Find closest greater value for every element in array in C++
- Find closest smaller value for every element in array in C++
- Minimum value among AND of elements of every subset of an array in C++
- Add every element of a masked Array with a scalar value in NumPy
- Subtract a scalar value from every element of a masked Array in NumPy
- Divide a scalar value into every element of a masked Array in NumPy
- Sum all duplicate value in array - JavaScript

Advertisements