繁体   English   中英

如何在没有 static 主键的情况下迭代 json?

[英]How can I iterate over a json without static main key?

I'm trying to iterate over a json object with multiple json objects inside, but the main key who allow me to access to the objects is not static , How can I iterate over those objects and at the same time to know the main key who允许我访问 object 吗?

提前致谢

let res = {
     Zw== : { price: "500", code: "Zw==", name: "Word 2013", image: "path/d360f0.jpg", total: 1},
     aA== : { price: "300", code: "aA==", name: "Power point 2013", image: "path/80fa572.png", total: 1},
     xH== : { price: "300", code: "aA==", name: "Power point 2013", image: "path/80fa572.png", total: 1}
};

你的 object 属性看起来坏了,所以我会调查一下。

我认为您正在寻找的是Object.keys ..

例子:

function printKeys(obj) {
    console.log(Object.keys(obj));
}

你可以传入你的res object,它会给你 object 的属性键。

从那里您可以使用该属性作为下一个键。

printKeys(res['Zw']);

JSFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM