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,
  }
}
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]);
};
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"]);
        }
    }

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.