我有像这张图片这样的 json 数据,它是我从 PHP 获得的。
我想访问来自 json 的所有数据。 对于我当前的情况,我想将现有数据与来自 Json 的数据进行匹配。
但是我有一点错误。我的所有数据都与 Json 数据不同。 虽然必须有一些合适的数据。
这是我的代码
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);
有人能帮帮我吗?
最佳答案
如果您只是想在数组中的对象中搜索特定的键 - 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/