converting an array of values

node v10.24.1
version: 1.0.0
endpointsharetweet
This notebook shows how to take and returns the value formatted in Roman Numerals.
var romanize = require('romanize'); const input = { "amounts": [{ "value": 124 }, { "value": 3422 } ] };
Example 1: Use a for-loop to create a new array with the converted values
// Read more about how this works at: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration // create a new array let convertedResults = []; // loop over the input array for (var i = 0; i < input.amounts.length; i++) { // take the element at the i index of the array var element = input.amounts[i]; var convertedElement = romanize(element.value); convertedResults.push(convertedElement); } // let's look at the new array in RunKit: convertedResults
Example 2: Map over the elements and convert each value in place
// Read more about how this works at: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Syntax // map over the input array input.amounts.map(function (input) { // uncomment the line below to log each array element value to the console // console.log(input.value); return romanize(input.value); }) // this is a lot more concise, and it uses less memory because we're replacing the values instead of creating a new array
Loading…

no comments

    sign in to comment