
- 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
Construct objects from joining two strings JavaScript
We are required to write a JavaScript function that takes in two comma separated strings. The first string is the key string and the second string is the value string, the number of elements (commas) in both the strings will always be the same.
Our function should construct an object based on the key and value strings and map the corresponding values to the keys.
Example
const str1= '[atty_hourly_rate], [paralegal_hourly_rate], [advanced_deposit]'; const str2 = '250,150,500'; const mapStrings = (str1 = '', str2 = '') => { const keys = str1.split(',').map( (a) => { return a.slice(1, -1); }); const object = str2.split(',').reduce( (r, a, i) => { r[keys[i]] = a; return r; }, {}); return object; }; console.log(mapStrings(str1, str2));
Output
And the output in the console will be −
{ atty_hourly_rate: '250', paralegal_hourly_rate: '150', advanced_deposit: '500' }
- Related Articles
- Joining two strings with two words at a time - JavaScript
- Joining strings to form palindrome pairs in JavaScript
- Joining two Arrays in Javascript
- Joining two hash tables in Javascript
- Longest possible string built from two strings in JavaScript
- Difference between two strings JavaScript
- JavaScript Union of two objects
- JavaScript: create an array of JSON objects from linking two arrays
- Differences in two strings in JavaScript
- How to merge two JavaScript objects?
- Construct a record array from a wide-variety of objects in Numpy
- Construct K Palindrome Strings in C++
- Maximum absolute difference of the length of strings from two arrays in JavaScript
- How to concatenate two JavaScript objects with plain JavaScript ?
- Missing letter from strings in JavaScript

Advertisements