
- 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
JavaScript - Convert an array to key value pair
Suppose we have an array like this −
const arr = [ {"name": "Rahul", "score": 89}, {"name": "Vivek", "score": 88}, {"name": "Rakesh", "score": 75}, {"name": "Sourav", "score": 82}, {"name": "Gautam", "score": 91}, {"name": "Sunil", "score": 79}, ];
We are required to write a JavaScript function that takes in one such array and constructs an object where name value is the key and the score value is their value.
Use the Array.prototype.reduce() method to construct an object from the array.
Example
Following is the code −
const arr = [ {"name": "Rahul", "score": 89}, {"name": "Vivek", "score": 88}, {"name": "Rakesh", "score": 75}, {"name": "Sourav", "score": 82}, {"name": "Gautam", "score": 91}, {"name": "Sunil", "score": 79}, ]; const buildObject = arr => { const obj = {}; for(let i = 0; i < arr.length; i++){ const { name, score } = arr[i]; obj[name] = score; }; return obj; }; console.log(buildObject(arr));
Output
This will produce the following output in console −
{ Rahul: 89, Vivek: 88, Rakesh: 75, Sourav: 82, Gautam: 91, Sunil: 79 }
- Related Articles
- Appending a key value pair to an array of dictionary based on a condition in JavaScript?
- JavaScript - Sort key value pair object based on value?
- JavaScript Convert an array to JSON
- How to store a key => value array in JavaScript?
- JavaScript - convert array with null value to string
- Java Program to remove key value pair from HashMap?
- Add a key value pair to dictionary in Python
- Add key-value pair in C# Dictionary
- Get max value per key in a JavaScript array
- JavaScript: How to Create an Object from Key-Value Pairs
- Finding matching pair from an array in JavaScript
- How to convert an array into a complex array JavaScript?
- How to convert an array into JavaScript string?
- Find specific key value in array of objects using JavaScript
- Swift Program to Find Minimum Key-Value Pair in the Dictionary

Advertisements