当我打印数组时,同样的对象出现在数组中请检查。当我打印obj然后obj.dataArray包含相同的临时对象时

时间:2018-02-27 15:23:32

标签: jackson

请检查我是否打印obj然后在数据数组中相同的元素被插入两次而不是不同。如果n = 2,则第一个元素被推两次。

genLLEles(n, orgObj, homeSecObj, defaultElementObj, previousElements, socket) {
    let that = this;
    let arr = [];
    let obj = {
        dataArray: [],
        socketId: socket.USER_ID,
        requestId: "/sync#" + socket.id + socket.USER_ID + "#" + new Date().getTime(),
        moduleName: "ORG",
        action: "INSERT",
        userId: socket.USER_ID
    };
    console.log("\n\ should be oce");
    for (let i = 0; i < n; i++) { //n defines count of how many elements to be created
        let temp = defaultElementObj; //selected object to insert
        let _id = homeSecObj.KEY_VAL + "_TSK:" + defaultElementObj.SUB_KEY_TYPE + "_" + Date.now() + "_" + Math.floor(Math.random() * (10000 - 1)) + 1;
        console.log(obj.dataArray.length);
        if (obj.dataArray.length == 0) {
            temp.CML_PREVIOUS_ID = previousElements.length <= 0 ? "-1" : that.getPreObj(previousElements).KEY_VAL;
            temp.CML_NEXT_ID = "1";
            temp.ORG_ID = orgObj.ORG_ID;
            temp.DEPT_ID = orgObj.DEPT_ID;
            temp.KEY_VAL = _id;
            temp.CML_ID = _id;
            temp.CML_REF_ID = homeSecObj.KEY_VAL;
            temp.ACTIVE_STATUS = "1";
            temp.CML_IMAGE_PATH = "";
            temp.SYNC_PENDING_STATUS = "1";
        } else {
            obj.dataArray[obj.dataArray.length - 1].CML_NEXT_ID = _id;
            console.log("HERE", obj.dataArray[obj.dataArray.length - 1]);
            temp.CML_PREVIOUS_ID = obj.dataArray.length < 0 ? "-1" : obj.dataArray[obj.dataArray.length - 1].KEY_VAL;
            temp.CML_NEXT_ID = "1";
            temp.ORG_ID = orgObj.ORG_ID;
            temp.DEPT_ID = orgObj.DEPT_ID;
            temp.KEY_VAL = _id;
            temp.CML_ID = _id;
            temp.CML_REF_ID = homeSecObj.KEY_VAL;
            temp.ACTIVE_STATUS = "1";
            temp.CML_IMAGE_PATH = "";
            temp.SYNC_PENDING_STATUS = "1";
        }

        obj.dataArray.push(temp);


    }
    console.log("\n\genLLEles ==>  ", obj, "\n\n");
    /*return obj;*/
}

1 个答案:

答案 0 :(得分:0)

从上面的代码中,if和else中的值都来自相同的参数。因此,您将多次向dataArray中插入相同的值。