in Education by
i have an array of objects in [ { "country": "USA", "payment": [ { "paymentType": "Visa", "count": 1000 }, { "paymentType": "MasterCard", "count": 1000 } ] }, { "country": "CANADA", "payment": [ { "paymentType": "Visa", "count": 1000 }, { "paymentType": "MasterCard", "count": 1000 } ] }, {...} ] I want to transform it into [ ["USA" , "VISA" , 10000 ], ["USA","Mastercard",1000], [countryName2, paymentType,count] , ... ] Can someone help me>? I tried doing : const data2 = data.map(function(elem) { return [ elem.country, elem.payment.map(b=> b.paymentType), elem.payment.map(c=>c.count), ] }) but the output i get is not what i want, if someone could show how to do it, 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 can do this with a nested map where each array entry is created by extracting data from payment prefixed with the outer country. const data = [{"country":"USA","payment":[{"paymentType":"Visa","count":1000},{"paymentType":"MasterCard","count":1000}]},{"country":"CANADA","payment":[{"paymentType":"Visa","count":1000},{"paymentType":"MasterCard","count":1000}]}]; const data2 = data.flatMap(({ country, payment }) => payment.map(({ paymentType, count }) => [ country, paymentType, count ]) ); console.log(data2); .as-console-wrapper { max-height: 100% !important; } Run code snippetExpand snippet The flatMap() on the outer map removes the extra level of nesting created in the result.

Related questions

0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    This is my object. values : { title : 'this is title ..', translate : { en : 'some texts ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I was unable to find a solution that is identical with the issue I am trying to solve. If is a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I try to merge some object into one array I did output by q = [["99","99","99"],["9" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I store all of the Google Maps marker objects in a single array. Right now I am trying to set up ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    Suppose you have designed a Big Data batch using the MapReduce framework. Now you want to execute it on a cluster ... Run view? Name Node Data Node Resource Manager Job Tracker...
asked Mar 23, 2021 in Technology by JackTerrance
0 votes
    I am building a basic RSS reader app that, which the 'story' is clicked, the information for it is displayed in a modal ... .log('Does Not Work!'); Or another way: - overscroll...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    An unhandled exception of type System.AggregateException occurred in mscorlib.dll Inner Exception: {"Response status ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I'm trying to use createCriteria with OR on two nested objects. This is the code I'm working with ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a vue.js application and into a componente there's the data method thats returns a nested object ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Hi can somebody help Removing element from nested json array like this JSON [{ "id": 1, "name": ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    The primary purpose of the array map() function is that it __________ (a) maps the elements of another ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...