How to get matching elements from array and change array object?

hussain

if array has matching ids change object to one object without effecting original array with below code it returns only matching objects but i want expected result as mentioned

main.ts

const arr = [{
            "body": {
                "specialtyID": "7114798",
                "sourceSystem": "HBS",
                "rxInfos": [{
                    "drugNdc": "00445450085",
                    "rxNumber": "14678904"
                }]

            },
            {
                "body": {
                    "specialtyID": "7114798",
                    "sourceSystem": "HBS",
                    "rxInfos": [{
                        "drugNdc": "00004080085",
                        "rxNumber": "1459004"
                    }]
                }
            },
            {
                "body": {
                    "specialtyID": "7908398",
                    "sourceSystem": "HBS",
                    "rxInfos": [{
                        "drugNdc": "06789955085",
                        "rxNumber": "1478604"
                    }]
                }
            }

        ]


        const tempArray = arr;

        function arrayMatch(temp, arr) {
            const finalArray = [];
            arr.forEach((element) => {
                    tempArray.forEach((temp) => {
                            if (temp.speicaltyID === element.specialtyID) {
                                temp.rxInfos.forEach((rxInfo) => {
                                    element.rxInfos.push(rxInfo);
                                });
                            }
                        }
                        finalArray = arr;

                    });
                return finalArray

            }

expected output

 const arr = [{
                        "body": {
                            "specialtyID": "7114798",
                            "sourceSystem": "HBS",
                            "rxInfos": [{
                                    "drugNdc": "00445450085",
                                    "rxNumber": "14678904"
                                },
                                {
                                    "drugNdc": "00004080085",
                                    "rxNumber": "1459004"
                                }
                            ]
                        },
                        {
                            "body": {
                                "specialtyID": "7908398",
                                "sourceSystem": "HBS",
                                "rxInfos": [{
                                    "drugNdc": "06789955085",
                                    "rxNumber": "1478604"
                                }]
                            }
                        }

                    ]
KShewengger

You can try this method to achieve your desired result:

const result = arr.reduce((container, value) => {
    const compare     = ({ body }) => body.specialtyID === value.body.specialtyID;
    const isExists    = container.some(compare);
    const pushRxInfos = container.map(item => compare(item) ? item.body.rxInfos.push(...value.body.rxInfos) : item);

    isExists ? pushRxInfos : container.push(value);

    return container;
}, []);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove elements from array if not matching?

How to search in array of object and get only matching object from the array in MongoDB

How to get an array without duplicates from object elements

how to get missing elements from array of Object? suppose my array of object is coming from server as Follow

How can i get data from an array of object with matching key and combine the other results into array

JavaScript get elements from an object array that are not in another

How to get matching element from array?

how to get an array from a object?

MongoDB get only matching elements from nested array

Get the value that is not matched with matching element from array with two elements

How to remove elements from nested object array?

How to get count of elements matching a predicate in a javascript array?

How to select elements from array in Julia matching predicate?

Remove object from array using matching array

How to get a number of random elements from an array?

How to get elements from Json array in PostgreSQL

How to get all elements from this array with ruby?

How to get elements from an array of Objects (JavaScript)

MongoDB: how to get specific elements from array?

how to change json format from array to object?

Get all elements with matching id in array of id

Get the total value of the matching array elements

How to get get not matching objects from two json array in javascript

How to get these array of object from the given array of object?

How to change object into array

How to get more random elements from an array, than the array has?

How do I remove an object from an array with a matching property?

How to remove object from array except matching value?

How to merge two matching objects from different array into one object?