gpt4 book ai didi

javascript - 访问 javascript 对象失败

转载 作者:行者123 更新时间:2023-11-30 19:27:16 25 4
gpt4 key购买 nike

我有像这张图片这样的 json 数据,它是我从 PHP 获得的。 enter image description here

我想访问来自 json 的所有数据。对于我当前的情况,我想将现有数据与来自 Json 的数据进行匹配。

enter image description here

但是我有一点错误。我的所有数据都与 Json 数据不同。虽然必须有一些合适的数据。

enter image description here

这是我的代码

const dataArr = <?= $ar; ?>;
console.log(dataArr); //image 1
var mySVG = document.getElementById("alphasvg");
var svgDoc;
mySVG.addEventListener("load",function() {

svgDoc = mySVG.contentDocument.documentElement;
var xd = svgDoc.getElementsByTagName("rect");

for(i=0;i<xd.length;i++){
let get_a = xd[i].getAttribute('id');
for(x = 1; x<= 30; x++){
for(y=0; y<=4; y++){
if(get_a == 'rect' + x + '_child' + y){
//console.log(get_a); //img2
if(dataArr[(x-1)*y].idval.includes(get_a) == true){
console.log('yes u good');
} else {
console.log('error');
}
}
}
}
}
}, false);

有人能帮帮我吗?

编辑:console.log(get_a,dataArr[(x-1)*y]); 的结果 enter image description here我的数据不匹配。

最佳答案

如果您只是想在数组中的对象中搜索特定的键 - idval - 以及它的值,您可以使用简单的 for 循环,例如:

for (var a = 0; a < dataArr.length; a++) {
if (dataArr[a].idval == get_a) {
console.log("there it is", dataArr[a]);
break;
}
}

这是一个例子:

var dataArr = [{
id: "test",
lantai: 1,
status_asrama: "tersedia",
idval: "rect_child1"
},
{
id: "anothertest",
lantai: 2,
status_asrama: "tersedia",
idval: "rect_child2"
}, {
id: "finaltest",
lantai: 3,
status_asrama: "tersedia",
idval: "rect_child3"
}
];

let get_a = "rect_child2";

for (var a = 0; a < dataArr.length; a++) {
if (dataArr[a].idval == get_a) {
console.log("there it is", dataArr[a]);
break;
}
}

关于javascript - 访问 javascript 对象失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56779818/

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