【问题标题】:Processing one object into two results returns the object first processed in both return statements no matter what [duplicate]将一个对象处理成两个结果会返回在两个返回语句中首先处理的对象[重复]
【发布时间】:2020-07-24 17:24:31
【问题描述】:

我正在构建一个 firebase 生态系统,但我怀疑它与此无关。 这也是我在这里的第一个问题,我想请与我交流。

所以我有一个名为 articleObject 的对象,其中包含有关产品的信息。

let articleObject = {
title: 'title of a product',
tagline: 'tagline of a product'
}

然后我将 articleObject 发送到 GCP AutoML - 实体提取,将 articleObject 分成两部分并以两种不同的方式处理结果两次,因为其中一种保留了我需要的所有信息,而另一种实际上可用于 Firestore noSQL 数据库因为显然它是一个 s- 我的意思是它旨在支持具有简单查询的平面层次结构。


async function extractEntities(articleObject) {
            const projectId = 'suck-it-1454';
            const location = 'us-central1';
            const modelId = 'TEN657473824614fake';
            let content = `${articleObject.title} ${articleObject.tagline}`
            console.log(content);
            let simpleObject = articleObject;
            let complexObject = articleObject;
            const client = new PredictionServiceClient();
            // Construct request
            const request = {
                name: client.modelPath(projectId, location, modelId),
                payload: {
                    textSnippet: {
                        content: content,
                        mimeType: 'text/plain', // Types: 'test/plain', 'text/html'
                    },
                },
            };

            const [response] = await client.predict(request);
            //simple way
            for (const tagPayload of response.payload) {
                let entityType = tagPayload.displayName;
                let entityText = tagPayload.textExtraction.textSegment.content;
                let certainty = tagPayload.textExtraction.score;
                let currentCertainty;
                try {
                    currentCertainty = simpleObject[entityType][entityText]
                    if (currentCertainty > certainty){
                        continue;
                    } else {
                        simpleObject[entityType] = entityText;
                    }
                } catch(err) {
                    simpleObject[entityType] = entityText;
                    
                }
                
            }

            //complex way
            for (const tagPayload of response.payload) {
                let entityType = tagPayload.displayName;
                let entityText = tagPayload.textExtraction.textSegment.content;
                let certainty = tagPayload.textExtraction.score;
                console.log(`entityType: ${entityType}, entityText: ${entityText}, certainty: ${certainty}`);
                complexObject[entityType] = {[entityText]:certainty};
                console.log('-------------------');
                console.log(complexObject[entityType]);
                console.log('-------------------');
            }

            
            console.log(complexObject);
            console.log(simpleObject);
            await browser.close();

            return [complexObject, simpleObject]

        }

好的,所以复杂的方式与它有关,给我一个复杂的结果,其中对象的格式如下:

let complexObject = {
    title: 'title of a product',
    tagline: 'tagline of a product',
    Brand:{"brand":0.9999993},
    Manufacturer:{"manufacturer":0.958499993, "possiblemanufacturer2":0.66555444}
    Model:{"model":0.93719993}
}

简单的方法给了我

let simpleObject = {
    title: 'title of a product',
    tagline: 'tagline of a product',
    Brand:"brand",
    Manufacturer:"manufacturer",
    Model:"model"
}

但是 - 最后,在 browser.close() 子句之前的两个 console.log 语句给了我同一个该死的对象(相同)。我的问题是为什么?如果我颠倒 for 循环的顺序,即 //simple 方式和 //complex 方式,结果是相反的。两个结果对象始终相同,但变化的是结果是复杂对象还是简单对象。

我正在使用节点 13,而 Firebase 模拟器正在使用节点 10,如果这很重要的话。 我怀疑它与 firebase 有什么关系。

【问题讨论】:

    标签: javascript node.js firebase javascript-objects


    【解决方案1】:

    您会在控制台中看到同一个对象两次,因为两个变量 simpleObjectcomplexObject 引用内存中的同一个对象。

    let simpleObject = articleObject;
    let complexObject = articleObject;
    // from this point `simpleObject` and `complexObject` refer to the same object
    

    您可以通过使用扩展运算符克隆初始 articleObject 来解决此问题,例如:

    let simpleObject = { ...articleObject };
    let complexObject = { ...articleObject };
    // now `simpleObject` and `complexObject` refer to separate copies of `articleObject`
    

    【讨论】:

    • 谢谢 - 我以为是这样,但我的 google-fu 说 javascript 对象是彼此的副本,而不仅仅是一个指针。 (如果我什至知道指针到底是什么)
    • @polisen 不客气
    猜你喜欢
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2017-12-16
    相关资源
    最近更新 更多