使用Lodash基于ID将对象添加到父数组

时间:2017-10-26 14:45:51

标签: javascript json lodash

所以我遇到了一个问题 - 我正在从我们的内部API中获取一些数据,但它的格式不正确,我需要做我必须做的事情,所以我必须进行一些转换。 / p>

为此,我决定使用Lodash,但是现在我被困住了。

基本上,我正在处理订单,但有些产品是父产品的插件。到目前为止,我已经设法将这两种类型的产品分开,但我不知道如何将“addons”数组作为子项添加到具有匹配ID的父产品中。

这是我想要的输出的基本剥离示例:

{
    "order": {
        "orderLines: [
            {
                "orderId": "foo",
                "addons" [
                    {
                        ...
                    }
                ]
            },
            {
                ...
            }
        ]
    }
}

这是我目前的代码:

// TODO:
// Match addons to products based on "connectedTo" => "id", then add matching addons as a new array on parent object

// Base data
const data = {
    "order": {
        "shopOrderId": "19LQ89H",
        "createDate": "2017-10-24T13:09:22.325Z",
        "orderLines": [
            {
                "orderId": "19LQ89H",
                "product": {
                    "productName": "Paintball",
                },
                "id": "59ef3b8036e16f1c84787c1f",
                "stringId": "59ef3b8036e16f1c84787c1f"
            },
            {
                "orderId": "19LQ89H",
                "product": {
                    "productName": "Ølsmagning",
                },
                "id": "59ef3b8036e16f1c84787c20",
                "stringId": "59ef3b8036e16f1c84787c20"
            },
            {
                "orderId": "19LQ89H",
                "product": {
                    "productName": "CD-indspilning",
                },
                "id": "59ef3b8136e16f1c84787c21",
                "stringId": "59ef3b8136e16f1c84787c21"
            },
            {
                "orderId": "19LQ89H",
                "product": {
                    "productName": "Julefrokost",
                },
                "id": "59ef3b8236e16f1c84787c22",
                "stringId": "59ef3b8236e16f1c84787c22"
            },
            {
                "orderId": "19LQ89H",
                "product": {
                    "productName": "Hummer Limousine",
                },
                "id": "59ef3b8236e16f1c84787c23",
                "stringId": "59ef3b8236e16f1c84787c23"
            },
            {
                "orderId": "19LQ89H",
                "connectedTo": "59ef3b8236e16f1c84787c23",
                "product": {
                    "productName": "Ekstra kørsel 400",
                },
                "id": "59ef3b8236e16f1c84787c24",
                "stringId": "59ef3b8236e16f1c84787c24"
            },
            {
                "orderId": "19LQ89H",
                "connectedTo": "59ef3b8236e16f1c84787c23",
                "product": {
                    "productName": "Drikkevarer",
                },
                "id": "59ef3b8236e16f1c84787c25",
                "stringId": "59ef3b8236e16f1c84787c25"
            },
            {
                "orderId": "19LQ89H",
                "connectedTo": "59ef3b8236e16f1c84787c23",
                "product": {
                    "productName": "Drikkevarer",
                },
                "id": "59ef3b8236e16f1c84787c26",
                "stringId": "59ef3b8236e16f1c84787c26"
            },
            {
                "orderId": "19LQ89H",
                "connectedTo": "59ef3b8236e16f1c84787c22",
                "product": {
                    "productName": "Snaps ad libitum",
                },
                "id": "59ef3b8236e16f1c84787c27",
                "stringId": "59ef3b8236e16f1c84787c27"
            }
        ],
        "travelTimes": [
            {
                "id": "59ef3b8036e16f1c84787c1f-59ef3b8036e16f1c84787c20",
                "partyPlanFromEventId": "59ef3b8036e16f1c84787c1f",
                "partyPlanToEventId": "59ef3b8036e16f1c84787c20",
                "start": "2017-11-15T17:02:59",
                "end": "2017-11-15T17:30:00",
                "travelTimeString": "27 min.",
                "travelTimeMinutes": 28,
                "exceedsAvailableTime": false
            },
            {
                "id": "59ef3b8036e16f1c84787c20-59ef3b8136e16f1c84787c21",
                "partyPlanFromEventId": "59ef3b8036e16f1c84787c20",
                "partyPlanToEventId": "59ef3b8136e16f1c84787c21",
                "start": "2017-11-15T19:52:12",
                "end": "2017-11-15T20:00:00",
                "travelTimeString": "8 min.",
                "travelTimeMinutes": 8,
                "exceedsAvailableTime": false
            },
            {
                "id": "59ef3b8036e16f1c84787c20-59ef3b8236e16f1c84787c22",
                "partyPlanFromEventId": "59ef3b8036e16f1c84787c20",
                "partyPlanToEventId": "59ef3b8236e16f1c84787c22",
                "start": "2017-11-15T12:30:00",
                "end": "2017-11-15T13:00:00",
                "travelTimeString": "8 min.",
                "travelTimeMinutes": 8,
                "exceedsAvailableTime": true
            },
            {
                "id": "59ef3b8036e16f1c84787c20-59ef3b8236e16f1c84787c23",
                "partyPlanFromEventId": "59ef3b8036e16f1c84787c20",
                "partyPlanToEventId": "59ef3b8236e16f1c84787c23",
                "start": "2017-11-15T08:30:00",
                "end": "2017-11-15T09:00:00",
                "travelTimeString": "3 min.",
                "travelTimeMinutes": 4,
                "exceedsAvailableTime": true
            }
        ],
        "id": "59ef3b8236e16f1c84787c28",
        "stringId": "59ef3b8236e16f1c84787c28"
    }
}

// Transform data
const travelTimes = data.order.travelTimes.map(item => _.omit(item, ['id']) )
const orderLines = _.merge(data.order.orderLines, travelTimes)
const order = _.omit(data.order, ['orderLines', 'travelTimes'])
const orders = _.assign(order, { orderLines })

const addonGroups = _.groupBy(order.orderLines, 'connectedTo')
const addons = _.omit(addonGroups, 'undefined')
const products = _.pick(addonGroups, 'undefined')
const productGroups = _.groupBy(products.undefined, 'stringId')

console.log(productGroups) // All parent products
console.log(addons) // All addon products

const arr1 = _.values(_.flatMap(productGroups))
const arr2 = _.values(_.flatMap(addons))

Code on Codepen.io

非常感谢任何帮助! 如果我需要进一步详细解释,请告诉我。

1 个答案:

答案 0 :(得分:1)

不确定我是否理解预期的结果是什么,但我还是试了一下。

const orderLines = _(data.order.orderLines)
    .map(item => {
        if (!item.connectedTo) return _.assignIn(item, { addons: [] });

        const match = _.find(data.order.orderLines, { id: item.connectedTo });
        match.addons = match.addons || [];
        match.addons.push(item);

        return null;
    })
    .compact()
    .value();

检查输出:https://codepen.io/andreiho/pen/YEzQRd?editors=0012