gpt4 book ai didi

javascript - 如何获取对象的值

转载 作者:行者123 更新时间:2023-11-29 18:04:22 25 4
gpt4 key购买 nike

我正在获取元素位置属性并将其存储在一个变量中。当我在控制台中打印该变量时,我可以看到它是一个像这样的对象:

Object {.portsTool path.aggregate: Object, .portsTool rect.remove: Object, .portsTool .handleInPorts: Object, .portsTool .handleOutPorts: Object, .resizeTool .resize: Object…}.: Object.body: Objectheight: 250stroke: "#000000"width: 150__proto__: Object.inPorts .port-label: Object.inPorts circle: Object.moveTool .area: Object.moveTool .visual: Object.outPorts .port-label: Object.outPorts circle: Object.port-body: Object.portsTool .handleInPorts: Object.portsTool .handleOutPorts: Object.portsTool path.aggregate: Object.portsTool rect.remove: Object.resizeTool .resize: Objectrect: Objecttext: Object__proto__: Object

当我展开它时:

>: Object
.body: Object
height: 250
stroke: "#000000"
width: 150
__proto__: Object

我想从中得到宽度的值。我怎么做?我是 javascript 的新手

更新:

var border = border2.prop('attrs');
for (y in border)
{
console.log(border[y]);
}

使用上面的迭代打印这样的对象:

Object {d: "m0,5l5,0l0,-5l5,0l0,5l5,0l0,5-5,0l0,5l-5,0l0,-5l-5,0z", stroke-width: 2, stroke: "#000", fill: "#5F5"}
(index):527 Object {width: 15, height: 6, stroke-width: 3, stroke: "#000", fill: "#F55"…}
(index):527 Object {ref: ".body", ref-x: -30, ref-y: -40}
(index):527 Object {ref: ".body", ref-dx: 10, ref-y: -40}
(index):527 Object {d: "M 0,10l10,0l0,-10z M -2,13l15,0l0,-14l0,14", fill: "black", stroke: "black", ref: ".body", ref-dx: 4…}
(index):527 Object {d: "M 0,15l5,-3l0,6l-5,-3l30,0l-5,-3l0,6l5,-3l-15,0l0,15l-3,-5l6,0l-3,5l0,-30l-3,5l6,0l-3,-5l0,15", fill: "black", stroke: "black", ref: ".body", ref-x: -40…}
(index):527 Object {ref: ".moveTool .visual", ref-x: 0, ref-y: 0, cx: "15", cy: "15"…}
(index):527 Object {font-size: 14, text: "", ref-x: 0.5, ref-y: 0.5, ref: ".body"…}
(index):527 Object {stroke: "#fff", fill: "#F9F9F9", stroke-width: 1, opacity: 0.8}
(index):527 Object {magnet: false, fill: "#FFFFFF", stroke: "none"}
(index):527 Object {type: "input"}
(index):527 Object {type: "output"}
(index):527 Object {r: 3, magnet: true, stroke: "#000000"}
(index):527 Object {width: 150, height: 250, stroke: "#000000"}
(index):527 Object {x: -15, dy: 4, text-anchor: "end", fill: "#000000"}
(index):527 Object {x: 15, dy: 4, fill: "#000000"}

最佳答案

解决方案是获取对象的属性名称。您想要获取对象 portsTool 的宽度属性。所以你可以使用:

portsTool.width

为了扩展对象以查看如何调用它,您可以这样做:

for (x in myObj) {
console.log(myObj[x]);
}

关于javascript - 如何获取对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32458821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com