1

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"
                                }]
                            }
                        }

                    ]
2
  • What have you tried so far? Commented Dec 6, 2018 at 21:40
  • @Zze function is returning array only for matching objects Commented Dec 6, 2018 at 22:05

1 Answer 1

2

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;
}, []);
Sign up to request clarification or add additional context in comments.

3 Comments

yes that is the solution i was looking only issue is matching element is being created under another nested array , How it can be pushed to same level where rxInfos elements are in object
it is coming like [{ element1 }, [{ matched element }] ] matched element should not wrap in array
@hussain had updated my answer. Just need to use the spread operator to get the raw data from the array - .push(...value.body.rxInfos) and it will now be [{ element1 }, { matched element } ]

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.