javascript - 如何从数组中获取匹配元素并更改数组对象?

标签 javascript arrays angular typescript

如果数组有匹配的 id 将对象更改为一个对象而不影响原始数组,使用下面的代码它只返回匹配的对象,但我想要预期的结果如前所述

主要.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

            }

预期输出

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

                    ]

最佳答案

您可以试试这个方法来达到您想要的效果:

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;
}, []);

关于javascript - 如何从数组中获取匹配元素并更改数组对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53659950/

相关文章:

javascript - Vue 未使用 Axios 请求中的数据从数组更新 Vuetify 数据表

javascript - 在以下情况下,为什么下拉菜单没有根据从选择框中选择的值隐藏和显示?

javascript - angularjs ng-click 无法更改 $scope 变量?

javascript - 序列化多个动态部分的路由功能 Ember.js

javascript - React 中的 componentWillMount 中的常量声明?

php - 使用 array_diff(),如何从 array2 而不是 array1 中获取差异?

c - 我在这段代码中做错了什么?

java - 在 Angular6 中显示从 Rest API 接收到的 HashMap<String, Object>

Angular 5 Reactive Forms - 单选按钮组

angular - ng-bootstrap 是否导入所有组件