
- 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
Split keys and values into separate objects - JavaScript
Suppose, we have an object like this −
const dataset = { "diamonds":77, "gold-bars":28, "exciting-stuff":52, "oil":51, "sports-cars":7, "bitcoins":40 };
We are required to write a JavaScript function that takes one such object and returns an array of objects that have keys and their values splitted.
Therefore, for the above object, the output should be −
const output = [ {"asset":"diamonds", "quantity":77}, {"asset":"gold-bars", "quantity":28}, {"asset":"exciting-stuff", "quantity":52}, {"asset":"oil", "quantity":51}, {"asset":"bitcoins", "quantity":40} ];
Example
Following is the code −
const dataset = { "diamonds":77, "gold-bars":28, "exciting-stuff":52, "oil":51, "sports-cars":7, "bitcoins":40 }; const splitKeyValue = obj => { const keys = Object.keys(obj); const res = []; for(let i = 0; i < keys.length; i++){ res.push({ 'asset': keys[i], 'quantity': obj[keys[i]] }); }; return res; }; console.log(splitKeyValue(dataset));
Output
This will produce the following output on console −
[ { asset: 'diamonds', quantity: 77 }, { asset: 'gold-bars', quantity: 28 }, { asset: 'exciting-stuff', quantity: 52 }, { asset: 'oil', quantity: 51 }, { asset: 'sports-cars', quantity: 7 }, { asset: 'bitcoins', quantity: 40 } ]
- Related Articles
- JavaScript program to merge two objects into a single object and adds the values for same keys
- How to split Python dictionary into multiple keys, dividing the values equally?
- Add values of matching keys in array of objects - JavaScript
- Comparing objects in JavaScript and return array of common keys having common values
- Excel tutorial: split text, number, and date cells (separate into multiple columns)
- The Keys and values method in Javascript
- JavaScript Separate objects based on properties
- Maps in JavaScript takes keys and values array and maps the values to the corresponding keys
- Mapping values to keys JavaScript
- Split string into groups - JavaScript
- 8085 program to separate (or split) a byte into two nibbles
- Fetching JavaScript keys by their values - JavaScript
- How to Sort/Order keys in JavaScript objects ?
- Split string into equal parts JavaScript
- How to insert new keys/values into Python dictionary?

Advertisements