-3

How can I access the object obj properties using a for loop?

Example:

var obj = {
        id: 1,
        description: "This space is for description",
        severity: "This is severity",
        assignedTo: "Name of the assigned person",
        status: "Issue Status "
    }

Note: I am talking about this kind of loop for (var i =0;i <= obj.length;i++), not a for ... in loop. I want to display it using document.write(). Please, no jquery, only javascript.

Matt
  • 25,467
  • 18
  • 120
  • 187
Aseem Sharma
  • 149
  • 1
  • 3
  • 14
  • 1
    @Kinduser The option for `Object.keys()` is there so it should help the OP – Icepickle Apr 04 '17 at 14:51
  • 1
    `for..in` is only JavaScript and you can still do do `document.write()` Do you have a specific issue with `for...in`? – Nope Apr 04 '17 at 14:52
  • @Icepickle Whatever. I just mean that some users shouldn't abuse their rights to close the questions before actually reading it. – kind user Apr 04 '17 at 14:53
  • 1
    provided link does not contain my answer. I specifically asked `for` loop and I specified it really well about which `for` loop I am talking about. Provided link contains the answers which uses`for..in` loop and I mentioned in my question that I do not want to use `for..in` loop – Aseem Sharma Apr 04 '17 at 14:54
  • 1
    @Kinduser Either way, I can see it both ways, it helps the OP do what he wants to do, but it also doesn't help to have new versions of this question, as it is a pretty common one – Icepickle Apr 04 '17 at 14:54
  • 2
    this dupe [answer](http://stackoverflow.com/a/18202926/1447675) provided the use of `Object.keys`. it clearly states, the the result is an array with keys. then just loop through the array with for i ... – Nina Scholz Apr 04 '17 at 14:55
  • 5
    Possible duplicate of [Iterate through object properties](http://stackoverflow.com/questions/8312459/iterate-through-object-properties) Look at this answer in the duplicate using `Objects.keys()` ► http://stackoverflow.com/questions/8312459/iterate-through-object-properties/18202926#18202926 and `.foreach` can easily be replaced by `for i` on the result of `Object.keys()` – Nope Apr 04 '17 at 14:57
  • @Fran: That question isn't a duplicate. If you read more than just the title, it's asking how the `for-in` construct works, specifically how the identifier is able to relate to each property. –  Apr 04 '17 at 14:58
  • @squint How do you know what I read? I looked at this ► http://stackoverflow.com/questions/8312459/iterate-through-object-properties/18202926#18202926 and `.foreach` can easily be replaced by `for i` – Nope Apr 04 '17 at 15:00
  • 1
    maybe this question matches better: http://stackoverflow.com/questions/558981/getting-a-list-of-associative-array-keys, or this http://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – Nina Scholz Apr 04 '17 at 15:09
  • 2
    @squint, maybe the main question is, how to use document.write with arguments ...? – Nina Scholz Apr 04 '17 at 15:13
  • 1
    And today I got to know about the mean people on stackoverflow. I thought it is filled with people who are happy to help and share their knowledge. Seing those comments it makes 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 – Aseem Sharma Apr 04 '17 at 15:23
  • **I don't understand why this got downvoted. I think it deserves more upvotes!** Some people in SO seem to be not constructive. Yes, there is a similar question, but the answer below was the one that instantly helped me. So I gave a +1 to both the question and the answer. – Matt Mar 26 '18 at 12:12
  • What is the reasoning behind insisting on not using the for in loop? – Jesse Jun 26 '18 at 19:19

1 Answers1

2

You are able to use for loop together with Object.keys().

Note: As squint mentioned, it does not guarantee the right order.

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]]}<br>`);
}
kind user
  • 40,029
  • 7
  • 67
  • 77
  • @squint What I mean by _order_ is the order of keys depending on when they've been defined (or set)! – ibrahim mahrir Apr 04 '17 at 15:08
  • 2
    @ibrahimmahrir: Yes, the original definition order is what some may hope for, but I think Kind user's answer gave that warning. An implementation may indeed choose to honor that order, but we shouldn't rely on it. –  Apr 04 '17 at 15:12
  • 2
    ...I actually wish that JS implementations would randomize the order so that people don't have the illusion that it may be reliable. –  Apr 04 '17 at 15:16