JavaScript Object.entries在更新对象后不正常工作

huangapple go评论70阅读模式
英文:

Javascript Object.entries not work properly after updating an object

问题

我正在尝试获取所有对象以追加到一个 div 中,但我的更新值未出现。如果我在分配对象时分配值,Object.entries 就可以获取所有值。

我的对象:

var allObjects = {
    ivSetObject : {
        id: "ivSet",
        url: "../assets/img/ivSet.png"
    },
    gloveObject : {
        id: "glove",
        url: "../assets/img/glove.png"
    },
    tourniqueObject: {
        id: "tournique",
        url: "../assets/img/tournique.png"
    },
    trashObject: {
        id: "trash",
        url: "../assets/img/trash.png"
    },
    glove2Object: {
        id: "glove2",
        url: "../assets/img/glove.png"
    },
    ivSet2Object: {
        id: "ivSet2",
        url: "../assets/img/ivSet.png"
    }
};


var selectedObjects = {};

我在这里添加值:

outerDiv.onclick = function () {
        Object.defineProperty(selectedObjects, key, {
            value: value
        });

        createSelectedDivs();
    };

我在这里尝试调用值:

var selectedContainer = document.getElementById("selectedItems");
function createSelectedDivs() {
    console.log(selectedObjects);

    selectedContainer.innerHTML = "";

    for (const [key, value] of Object.entries(selectedObjects)) {
        console.log("aa");
        const outerDiv = document.createElement("div");
        outerDiv.className = "selectedItemCard";

        const imgDiv = document.createElement("img");
        imgDiv.src = value.url;
        outerDiv.appendChild(imgDiv);

        selectedContainer.appendChild(outerDiv);

    }
    
}
英文:

I am trying to get all object to append in a div but my updated values are not coming. If I assign a value while assigning my object, Object.entries can get all values.

My objects:

var allObjects = {
    ivSetObject : {
        id: "ivSet",
        url: "../assets/img/ivSet.png"
    },
    gloveObject : {
        id: "glove",
        url: "../assets/img/glove.png"
    },
    tourniqueObject: {
        id: "tournique",
        url: "../assets/img/tournique.png"
    },
    trashObject: {
        id: "trash",
        url: "../assets/img/trash.png"
    },
    glove2Object: {
        id: "glove2",
        url: "../assets/img/glove.png"
    },
    ivSet2Object: {
        id: "ivSet2",
        url: "../assets/img/ivSet.png"
    }
};


var selectedObjects = {};

I am adding values from here:

outerDiv.onclick = function () {
        Object.defineProperty(selectedObjects, key, {
            value: value
        });

        createSelectedDivs();
    };

I am trying to call values from here:

var selectedContainer = document.getElementById("selectedItems");
function createSelectedDivs() {
    console.log(selectedObjects);

    selectedContainer.innerHTML = "";

    for (const [key, value] of Object.entries(selectedObjects)) {
        console.log("aa");
        const outerDiv = document.createElement("div");
        outerDiv.className = "selectedItemCard";

        const imgDiv = document.createElement("img");
        imgDiv.src = value.url;
        outerDiv.appendChild(imgDiv);

        selectedContainer.appendChild(outerDiv);

    }
    
}

I can see selectedObjects in console.log but Object.entries(selectedObjects) is not working.

答案1

得分: 0

根据mdn文档的描述:

通过赋值进行常规属性添加会创建在属性枚举期间可见的属性(例如,使用for...in、Object.keys()等)。这种方法允许更改这些额外的细节以使其不同于默认值。默认情况下,使用Object.defineProperty()添加的属性既不可写,又不可枚举,也不可配置。

你的代码:

const obj = {}
Object.defineProperty(obj, 'key', {
  value: 'value'
})
console.log(Object.entries(obj))

使用 enumerable: true

const obj = {}
Object.defineProperty(obj, 'key', {
  value: 'value',
  enumerable: true
})
console.log(Object.entries(obj))
英文:

Based on mdn docs

> Normal property addition through assignment creates properties which show up during property enumeration (for...in, Object.keys(), etc.), [...]. This method allows these extra details to be changed from their defaults. By default, properties added using Object.defineProperty() are not writable, not enumerable, and not configurable.

Your code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {}
Object.defineProperty(obj, &#39;key&#39;, {
  value: &#39;value&#39;
})
console.log(Object.entries(obj))

<!-- end snippet -->

With enumarable: true:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {}
Object.defineProperty(obj, &#39;key&#39;, {
  value: &#39;value&#39;,
  enumerable: true
})
console.log(Object.entries(obj))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 20:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定