简体   繁体   English

Javascript 数组没有以某种方式发生突变

[英]Javascript array is not somehow mutated

I have an express server which I have GET and POST, and as original state, I have the following:我有一个快递服务器,我有 GET 和 POST,作为原始 state,我有以下内容:

let orders = [
        {
            sum: 0,
            purchases: [
                {
                    id: 1,
                    contractId: 55,
                    value: 100,
                },
                {
                    id: 2,
                    contractId: 55,
                    value: -100,
                }
            ]
        }
    ]
    
    // this function is running on GET request
    export const getOrders = (req, res) => {
    reply.status(200).send(orders)
    }

export const addOrder = (req, res) => {
const newOrder = req.body
orders.map((order) => {
    const correspondingorder = order.purchases.find(
        (purshase) => purshase.contractId === newOrder.contractId
    )
    if (correspondingorder) {

        order.purchases.push(newOrder)
        order.sum += newOrder.value
    } else {
        const newValues = { sum: newOrder.value, purchases: Array(newOrder) }
        orders.push(newValues)
    }
}

} }

My intention here is to search in the list of orders if the new added order ID is exist, if so, then add it to the list of corresponding purchases, otherwise, create a new object containing a sum and the new order, but whenever I try to add a new order with the same id then it add it to the found contractID PLUS creating a new object containing a sum and new order.我的目的是在订单列表中搜索是否存在新添加的订单 ID,如果存在,则将其添加到相应的购买列表中,否则,创建一个包含总和和新订单的新 object,但每当我尝试添加具有相同 id 的新订单,然后将其添加到找到的 contractID PLUS 创建一个包含总和和新订单的新 object。

here is an example of what I get after my first POST and then GET:这是我在第一次 POST 和 GET 后得到的示例:

[
    {
        "sum": 100,
        "purchases": [
            {
                "id": 1,
                "contractId": 55,
                "value": 100
            },
            {
                "id": 2,
                "contractId": 55,
                "value": -100
            },
            {
                "id": 3,
                "contractId": 55,
                "value": 100
            }
        ]
    }
]

then my second attempts of adding a different order with POST:然后我第二次尝试使用 POST 添加不同的订单:

[
    {
        "sum": 100,
        "purchases": [
            {
                "id": 1,
                "contractId": 55,
                "value": 100
            },
            {
                "id": 2,
                "contractId": 55,
                "value": -100
            },
            {
                "id": 3,
                "contractId": 55,
                "value": 100
            }
        ]
    },
    {
        "sum": 100,
        "purchases": [
            {
                "id": 3,
                "contractId": 44,
                "value": 100
            }
        ]
    }
]

then another post and got this results:然后另一个帖子并得到了这个结果:

[
    {
        "sum": 100,
        "purchases": [
            {
                "id": 1,
                "contractId": 55,
                "value": 100
            },
            {
                "id": 2,
                "contractId": 55,
                "value": -100
            },
            {
                "id": 3,
                "contractId": 55,
                "value": 100
            }
        ]
    },
    {
        "sum": 200,
        "purchases": [
            {
                "id": 3,
                "contractId": 44,
                "value": 100
            },
            {
                "id": 4,
                "contractId": 44,
                "value": 100
            }
        ]
    },
    {
        "sum": 100,
        "purchases": [
            {
                "id": 4,
                "contractId": 44,
                "value": 100
            }
        ]
    }
]

any idea why this weird behavior is happening?知道为什么会发生这种奇怪的行为吗?

This is because orders.map returns a new object and doesn't mutate the original, you can override the original with:这是因为 orders.map 返回一个新的 object 并且不会改变原始文件,您可以使用以下命令覆盖原始文件:

orders = orders.map((order) => {
        const correspondingorder = order.purchases.find(
            (purshase) => purshase.contractId === newOrder.contractId
        )
        if (correspondingorder) {
            order.purchases.push(newOrder)
            order.sum += newOrder.value
        } else {
            const newValues = { sum: newOrder.value, purchases: Array(newOrder) }
            orders.push(newValues)
        }
    }
}

But honestly, I don't recommend mutating Objects in this context, because more than 1 request can be reading/writing that variable.但老实说,我不建议在这种情况下改变对象,因为超过 1 个请求可以读取/写入该变量。

If you really need to use this approach, use Array.forEach and reference the original object instead of map (that returns a new array and doesn't mutate the original one)如果您确实需要使用这种方法,请使用 Array.forEach 并引用原始 object 而不是 map (返回一个新数组并且不会改变原始数组)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM