Access javascript object properties using loop
如何使用for循环访问对象obj属性?
例子:
1 2 3 4 5 6 7
| var obj = {
id: 1,
description:"This space is for description",
severity:"This is severity",
assignedTo:"Name of the assigned person",
status:"Issue Status"
} |
注:我说的是这种循环for (var i =0;i <= obj.length;i++),不是for ... in循环。我想用document.write()显示它。请不要使用jquery,只使用javascript。
- @Kinduser the option for EDOCX1 commercial 0 is there so it should help the op
- EDOCX1是唯一的Javascript and you can still do EDOCX1-2
- 冰刀Whatever。I just mean that some users should not abuse their rights to close the questions before actually reading i t.
- Provided link does not contain my answer.I specifically asked EDOCX1 penographic 4 sili loop and I specified it really well about which EDOCX1 penographic 4.Provided link contains the answers which used sedox1 silios loop and I mentioned in my question that I do not want to use EDOCX1 silios loop
- @Kinduser either way,I can see i t both ways,i t helps t he OP do what he wants to do,but i t also doesn't help to have new versions of this question,as i t is a pretty common one
- 这个双倍的答案提供了使用EDOCX1It clearly States,the result is an array with keys.他们就在我身边徘徊着…
- 是否有可能在使用EDOCX1字体9的复制品中,通过目标财产看这个答案?Stackoverflow.com/Questions/8312459/&Hellip;和EDOCX1的英文字母10可以轻松地被EDOCX1的英文字母11替换为关于EDOCX1的结果
- @Fran:That question is't a duplicate.如果你读到的比所有权还多,那就请你说明EDOCX1的英文第13篇建筑工程如何能够具体说明身份如何能够与each property相关。
- @Squint how do you know what I read?我看这个?Stackoverflow.com/questions/8312459/&Hellip;and EDOCX1 Big 10 Can Easily be replaced by EDOCX1
- Maybe this question matches better:stackovlow.com/questions/558981/&Hellip;,or this stackoverflow.com/questions/684672/&Hellip;
- @Squint,maybe the main question is,how to use document.write with arguments…?
- 而今天我知道关于Stackoverflow的人。我认为,这是与人谁是快乐的帮助和分享他们的知识。∮It comments it make me wonder people who are complaining actually have time to write those comments saying whatever they want but cannot write an answer which could've been much simpler than writing those comments
- 我不明白为什么要这么做我想你需要更多的支持!Some people in so seem to be not constructive.是的,有一个类似的问题,但答案是那个瞬间帮助我的人。So I gave a+1 to both the question and the answer.
- What is the reasoning behind insisting on not using the for in loop?
您可以使用for循环和Object.keys()循环。
注:正如斜视提到的,它不能保证正确的顺序。
1 2 3 4 5 6 7 8 9 10 11
| var obj = {
id: 1,
description:"This space is for description",
severity:"This is severity",
assignedTo:"Name of the assigned person",
status:"Issue Status"
}, elems = Object.keys(obj);
for (var i = 0; i < elems.length; i++) {
document.write(`Key: ${elems[i]}, Value: ${obj[elems[i]]}`);
} |
- @我所说的顺序是指键的顺序,这取决于它们被定义(或设置)的时间!
- @易卜拉欣:是的,最初的定义顺序可能是一些人希望的,但我认为善良的用户的回答给出了警告。一个实现可能确实选择遵守这个命令,但我们不应该依赖它。
- …我真的希望JS实现能够随机化顺序,这样人们就不会有这样的错觉:它可能是可靠的。