0

I have the following array

[ {
            "contactId": "a87d096gd5fuop",
            "firstName": "John Doe",

            "registrationTypes": {
                "selectedOptions": [
                    {
                    }
                ],
                "subTotal": 1620.003
            },
            "foo1": {
                "selectedOptions": [
                ],
                "subTotal": 0
            },
         
            "events": {
                "selectedOptions": [
                    {
                        "id": "1",
                        "name": "T1",
                        "value": "4550006:3",
                    },
                    {
                        "id": "2",
                        "name": "T2",
                        "value": "4550005:3",
                    },
                    {
                        "id": "3",
                        "name": "T3",
                        "value": "4550003:3",
                    }
                ],
                "subTotal": 135.003
            },
            "freeNetworkingFunctions": {
            
            },

            "total": 1755.0059999999999
        },
        {
            "contactId": "a097f",
            "firstName": "David",

            "registrationTypes": {
                "selectedOptions": [
                   
                  {}
                ],
                "subTotal": 899.998
            },
            "foo1": {
                "selectedOptions": [
                 
                ],
                "subTotal": 0
            },
            "member": {
                "selectedOptions": [
                    {
                        
                    }
                ],
                "subTotal": 228.8
            },
            "events": {
                "selectedOptions": [
                    {
                        "id": "4",
                        "name": "T4",
                        "value": "4550002:2",
                     
                    },
                    {
                        "id": "5",
                        "name": "T5", 
                        "value": "4550001:2",
                       
                    },
                    {
                        "id": "6",
                        "name": "T6",
                        "value": "4550003:2",
                       
                    }
                ],
                "subTotal": 135.003
            },
           
            "total": 1263.801
        }
    ]

From the above array, I want to extract events, loop all the data and get only values. So my new array should be something like this:

[ {
    "contactId": "a87d096gd5fuop",
    "firstName": "John Doe",

    "registrationTypes": {
        "selectedOptions": [
            {
            }
        ],
        "subTotal": 1620.003
    },
    "foo1": {
        "selectedOptions": [
        ],
        "subTotal": 0
    },
 
    "events": [
        "4550006:3"
        "4550005:3",
        "4550003:3", 
    ],      

    },
    "freeNetworkingFunctions": {
    
    },

    "total": 1755.0059999999999
},
{
    "contactId": "a097f",
    "firstName": "David",

    "registrationTypes": {
        "selectedOptions": [
           
          {}
        ],
        "subTotal": 899.998
    },
    "foo1": {
        "selectedOptions": [
         
        ],
        "subTotal": 0
    },
    "member": {
        "selectedOptions": [
            {
                
            }
        ],
        "subTotal": 228.8
    },
    "events": [
        "4550004:2"
        "4550008:3",
        "4550003:3", 
    ],    
        "subTotal": 135.003
    },
   
    "total": 1263.801
}
]

So it should return the original array, however, events value data should be in one array.

 var arr = [];
 var r(var i=0;i<data.length;i++){
    data.push(arr[i].value);
 }
 var newData = [...data, arr]

However, this doesn't work. Any help would be highly appreciated.

1 Answer 1

3

Use map twice - once on the dataset to iterate over the objects, and within that map to get an array of values from the selectedOptions.

const data=[{contactId:"a87d096gd5fuop",firstName:"John Doe",registrationTypes:{selectedOptions:[{}],subTotal:1620.003},foo1:{selectedOptions:[],subTotal:0},events:{selectedOptions:[{id:"1",name:"T1",value:"4550006:3"},{id:"2",name:"T2",value:"4550005:3"},{id:"3",name:"T3",value:"4550003:3"}],subTotal:135.003},freeNetworkingFunctions:{},total:1755.0059999999999},{contactId:"a097f",firstName:"David",registrationTypes:{selectedOptions:[{}],subTotal:899.998},foo1:{selectedOptions:[],subTotal:0},member:{selectedOptions:[{}],subTotal:228.8},events:{selectedOptions:[{id:"4",name:"T4",value:"4550002:2"},{id:"5",name:"T5",value:"4550001:2"},{id:"6",name:"T6",value:"4550003:2"}],subTotal:135.003},total:1263.801}];

const out = data.map(obj => {
  
  // Destructure the selected options from the
  // rest of each object
  const { events: { selectedOptions }, ...rest } = obj;
  
  // `map` over the options to just get an array of values
  const events = selectedOptions.map(option => {
    return option.value;
  });
  
  // Return a new object with the new events property
  // combined with the other properties again
  return { ...rest, events };

});

console.log(out);

Additional documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.