1

I am using this code

var obj  =  {   'name':'Some Person Name',
                'country':'Country of Some Person',
                'age':1
            }
var storeVal = Object.keys(obj)

for (var i = 0; i < storeVal.length; i++) {
    var storeLoop = storeVal[i] ;
    document.write('<pre>'+storeLoop+'</pre>');
}

Now I can display the keys of object obj using above code and I also know how to display values of object obj using Object.values(). I want to know how I can display the whole object obj using above for loop and I am not talking about for..in loop. I want to also know how to display the specific key and value,for example if I want to display only name key and its value,how that can be done ? I am only using JavaScript. No jquery.

5
  • You keep saying you don't want a for-in loop, but then you use constructs that provide nearly the same semantics as for-in. Why is that? Commented Apr 4, 2017 at 18:39
  • @squint I am in my initial stage of learning JavaScript. I did not even use for..in loop in my entire learning since I started learning JavaScript. I do not know about the constructs of for..in, I just want to understand only for loop properly and then I'll go for variations like for..in. I am just freaking clearing my concepts properly. Commented Apr 5, 2017 at 12:41
  • Settle down pal. I'm the one who was fighting to keep your previous question open. But if you ask how to do things that are inherently misguided, you're going to get people inquiring about them so that they don't send you down the wrong path. If you want to understand for properly, that's great, but that has nothing to do with enumerating objects. Commented Apr 5, 2017 at 12:45
  • I know you were on my side and I appreciate that. Thanks for that and I will keep your point in mind when I ask questions next time. Commented Apr 5, 2017 at 12:52
  • No problem. Best of luck! Commented Apr 5, 2017 at 12:53

4 Answers 4

2

Here's one way to do it:

var obj  =  {   'name':'Some Person Name',
                'country':'Country of Some Person',
                'age':1
            }
var storeVal = Object.keys(obj)

for (var i = 0; i < storeVal.length; i++) {
    var storeLoop = storeVal[i] + ': ' + obj[storeVal[i]];
    document.write('<pre>'+storeLoop+'</pre>');
}

// Just displaying a certain key:

document.write('<pre>Name: '+obj.name+'</pre>');

Sign up to request clarification or add additional context in comments.

Comments

2

I suggest to use variable names which reflects their content, then I suggest to declare all variables first, an not for example inside of a for loop.

For getting the value of a property of an object, you could use one of two syntax as property accessor

object.property     // dot notation
object['property']  // bracket notation

To get a value of the object, you could use

object.name  // 'Some Person Name'

var object = { name: 'Some Person Name', country: 'Country of Some Person', age: 1 },
    keys = Object.keys(object),
    value,
    i, 
    nameKey = 'name';

for (i = 0; i < keys.length; i++) {
    value = object[keys[i]];
    document.write('<pre>' + keys[i] + ': ' + value + '</pre>');
}

document.write('<hr><pre>' + nameKey + ': ' + object[nameKey] + '</pre>');

1 Comment

Thanks for your time but I wanted to display the whole object like @Schlaus done in his answer.
0

If it is just for you and you don't really want to output it in the HTML code, you can use console.log(storeVal); and explore the printed object in your browser console.

See more here: https://developer.mozilla.org/en-US/docs/Web/API/Console/log

Comments

0

Use Object.entries() , see MDN-Doc

Basic example:

var obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]


Your code:

var obj  =  {   'name':'Some Person Name',
                'country':'Country of Some Person',
                'age':1
            }

Object.entries(obj).forEach(([key, value]) => {
    // Do whatever you like with key and value
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.