gpt4 book ai didi

javascript - 在 JavaScript/TypeScript 中迭代 Json 数据

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:00 24 4
gpt4 key购买 nike

我需要迭代此 Json 数据并在 JavaScript(TypeScript) 中向网格添加值,无需 Jquery。

{"GridHeader":{"Id":"Id","Name":"Full Name","Age":"Age"},"GridData":{"Id":3,"name":"Vu","age":34}}

我有如下添加函数,它向网格添加标题和数据:

 let header = '{"GridHeader":{"Id":"Id","Name":"Full Name","Age":"Age"},"GridData":{"Id":3,"name":"Vu","age":34}}';
let myheader = JSON.parse(header);


for (var i = 0; ??) {
....

AddRecord(headerdata, i);
}

这是我将它添加到网格的地方:

function AddRecord(record, n) {
var detail = document.getElementById("detail");
var header = document.getElementById("header");

if (n == 0) {
var newdiv = document.createElement("div");
newdiv.innerHTML = "<div style=''>" + record.id + "</div>" + "<div>" + record.name + "</div>" + "<div>" + record.age + "</div>";
}

var newdiv = document.createElement("div");
newdiv.innerHTML = "<div style=''>" + record.id + "</div>" + "<div>" + record.name + "</div>" + "<div>" + record.age + "</div>";
detail.appendChild(newdiv);

最佳答案

是的,你的数据实际上是下面的一个javascript对象:

var header = {
"GridHeader":{"Id":"Id","Name":"Full Name","Age":"Age"},
"GridData": {"Id":3,"name":"Vu","age":34}
};

你可以像下面这样遍历它:

for (var prop in header) {
console.log("Key:" + prop);
console.log("Value:" + header[prop]);
}

请根据您的需要制作。我已经给了你一个迭代它的线索。谢谢。

关于javascript - 在 JavaScript/TypeScript 中迭代 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42400298/

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