
- 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
map() array of object titles into a new array based on other property value JavaScript
Let’s say, we have an array of objects like this −
const arr = [{ country: "cananda", count: 2 }, { country: "jamaica", count: 2 }, { country: "russia", count: 1 }, { country: "india", count: 3 }, { country: "spain", count: 2 }, { country: "portugal", count: 1 }, { country: "italy", count: 1 }];
We are required to write a function that takes in this array, maps over it and returns an array of strings with country names repeated “count” number of times for each particular object.
Therefore, the output of the function for this object should be −
['canada', 'canada', 'jamaica', 'jamaica', 'russia', 'india', 'india', 'india','spain', 'spain','portugal', 'italy']
Let’s write the code for this function. We will use the Array.prototype.reduce() method here −
Example
const arr = [{ country: "canada", count: 2 }, { country: "jamaica", count: 2 }, { country: "russia", count: 1 }, { country: "india", count: 3 }, { country: "spain", count: 2 }, { country: "portugal", count: 1 }, { country: "italy", count: 1 }]; const repeatCount = (arr) => { return arr.reduce((acc, val) => { let { count, country } = val; while(count--){ acc.push(country); } return acc; }, []); }; console.log(repeatCount(arr));
Output
The output in the console will be −
[ 'canada', 'canada', 'jamaica', 'jamaica', 'russia', 'india', 'india', 'india', 'spain', 'spain', 'portugal', 'italy' ]
- Related Articles
- How to add a new object into a JavaScript array after map and check condition?
- Sort object array based on another array of keys - JavaScript
- Sum of array object property values in new array of objects in JavaScript
- Filter an object based on an array JavaScript
- Manipulate Object to group based on Array Object List in JavaScript
- Grouping an Array and Counting items creating new array based on Groups in JavaScript
- Splitting an array based on its first value - JavaScript
- Sorting Array based on another array JavaScript
- Merge arrays into a new object array in Java
- Formatting JavaScript Object to new Array
- Sort array based on another array in JavaScript
- Filter array based on another array in JavaScript
- Modify an array based on another array JavaScript
- Map to create new value from int array in Java
- Build maximum array based on a 2-D array - JavaScript

Advertisements