in Education by
I'm new to JavaScript. I have the following object: let obj1 = [{ 'id': 1, 'longString': 'Joe - 2011-04-23T18:25:23.511Z' }, { 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z' }]; I want to split the longString key into 2 keys, and the value into 2 values. Here is what I want to make: let obj2 = [{ 'id': 1, 'name': 'Joe', 'date': '2011-04-23T18:25:23.511Z' }, { 'id': 2, 'name': 'Mary', 'date': '2010-04-23T18:25:23.511Z' }]; I've tried to do it in pieces, but I'm stuck with the first piece which is splitting the value of longString. This works for string: let v = 'Mary - 2010-04-23T18:25:23.511Z'; let w = v.split(' -', 1); How do I make it work for an object? Or is there a totally different way to split property values? Also, do I need to write a loop to assign obj1 to obj2? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You could map over the array and split the longString for each object. Use destructuring to get the splits into name and date variables let obj1 = [{ 'id': 1, 'longString': 'Joe - 2011-04-23T18:25:23.511Z' }, { 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z' }]; const output = obj1.map(a => { let [name, date] = a.longString.split(" - "); return { id: a.id, name, date } }) console.log(output) Run code snippetExpand snippet

Related questions

0 votes
    I'm new to JavaScript. I have the following object: let obj1 = [{ 'id': 1, 'longString': ' ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    What's the best way to extract the key and value from a string like this: var myString = 'A1234= ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 9, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    In a Key-Value datastore, both keys and values need to be unique. (1)True (2)False...
asked Apr 23, 2021 in Technology by JackTerrance
0 votes
    Have jquery dialog as closeOnescape as false. want to trigger an event based on esc key press how do ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    If needed to split a node or coalesce it with its siblings, or redistribute key values between siblings, ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    So the idea is that users can create collections with many pairs of words in it. For example, they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I am trying to return an javascript value with Awesome lib in Vb.net / C#. Now I know how to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    My requirement is once I click on checkbox and save/update textbox should be disabled and when I uncheck ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    My requirement is once I click on checkbox and save/update textbox should be disabled and when I uncheck ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    How can I get the td values with jquery? The while loops will generate a few with the same class ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I have an HTML table with a search field to filter the result by Name. And another field to show ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
...