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
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

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]);
};
Fecosos
  • 944
  • 7
  • 17
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"]);
        }
    }
Amal
  • 75
  • 1
  • 9