0

How can I loop on all of these (objects ? I don't know) with Javascript?

{
 test1: {
 name: "1",
 fa: true,
 },
 test2: {
 name: "2",
 fa: false,
 },
 test3: {
 name: "3",
 fa: true,
 }
}
Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Apr 30, 2018 at 1:47
5
  • Look up Object.entries, Object.values, Object.keys Commented Apr 30, 2018 at 1:47
  • I'm really not able to find anything, sorry .. Commented Apr 30, 2018 at 1:52
  • You can have a look at this Commented Apr 30, 2018 at 1:55
  • Hello @ThumChoonTat, I was on this post and did my best, but how can I get the key2 value ? Here is my code : pastebin.com/npwknHCA Thanks ! Commented Apr 30, 2018 at 2:02
  • Just like how you get the value of [key], p[key][key2] Commented Apr 30, 2018 at 2:04

2 Answers 2

2

You could use the returned array of Object.keys(object) or a for...in loop

const obj = {
 "test1": {
 "name": "1",
 "2fa": true,
 },
 "test2": {
 "name": "2",
 "2fa": false,
 },
 "test3": {
 "name": "3",
 "2fa": true,
 }
};
Object.keys(obj).map(key => {
 console.log(obj[key]);
});
for(const key in obj) {
 console.log(obj[key]);
};
answered Apr 30, 2018 at 2:03
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the following code to loop through these objects;

var objects={
 "test1": {
 "name": "1",
 "2fa": true,
 },
 "test2": {
 "name": "2",
 "2fa": false,
 },
 "test3": {
 "name": "3",
 "2fa": true,
 }
 }
 for (var key in objects) {
 if (objects.hasOwnProperty(key)) { 
 var obj= objects[key]
 console.log(key);
 console.log("Name: " + obj["name"]);
 console.log("2fa:" + obj["2fa"]);
 }
 }
answered Apr 30, 2018 at 2:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.