0

I have an array of objects. I want to get the key and value and print the data. eg. I have id, username and password and I want the username and password.

[
  {"id":1,"name":"admin","password":"admin","role":"Admin"},
  {"id":2,"name":"user","password":"user","role":"User"},
  {"id":3,"name":"superadmin","password":"superadmin","role":"superadmin"}
]

The output should be

name : admin
password : admin,
name : user
password : user,
name : superadmin
password : superadmin 
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Sam
  • 1,381
  • 4
  • 30
  • 70
  • Note that the logic for solving this request has nothing to do with AJAX or JSON, so I removed references to them. – Rory McCrossan Feb 26 '19 at 11:16
  • 1
    Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – vahdet Feb 26 '19 at 11:21
  • i have a login form when the user types the username and password. i want to check the data through API GET method .In that API i have those json data – Sam Feb 26 '19 at 11:42

4 Answers4

1
To iterate key values dynamically, you have to iterate object.

    let arr = [
      {"id":1,"name":"admin","password":"admin","role":"Admin"},
      {"id":2,"name":"user","password":"user","role":"User"},
      {"id":3,"name":"superadmin","password":"superadmin","role":"superadmin"}
    ];

    arr.forEach((item, i) => {
       for(let key in item) {
           console.log(`${key}: ${item[key]}`);
       }
    });

If you would like to display only name and password, then you can add a condition.

arr.forEach((item, i) => {
   for(let key in item) {
      if (key === 'name' || key === "password") {
          console.log(`${key}: ${item[key]}`);
      }
   }
});
Kurshith
  • 81
  • 1
  • 7
0

Given the array you have, you simply need to loop through it:

var arr = [
  {"id":1,"name":"admin","password":"admin","role":"Admin"},
  {"id":2,"name":"user","password":"user","role":"User"},
  {"id":3,"name":"superadmin","password":"superadmin","role":"superadmin"}
]

arr.forEach(function(obj) {
  console.log('name: ' + obj.name);
  console.log('password: ' + obj.password);
})
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0
let data = [
  {"id":1,"name":"admin","password":"admin","role":"Admin"},
  {"id":2,"name":"user","password":"user","role":"User"},
  {"id":3,"name":"superadmin","password":"superadmin","role":"superadmin"}
];

var out_data = data.reduce((a, b) => a + 'name: ' + b.name + ', password: ' + b.password + ' ', '');
console.log(out_data);
0
var arr = [
  {"id":1,"name":"admin","password":"admin","role":"Admin"},
  {"id":2,"name":"user","password":"user","role":"User"},
  {"id":3,"name":"superadmin","password":"superadmin","role":"superadmin"}
]
[![enter image description here][1]][1]
Loop through the array
arr.forEach(function(data) {
  console.log('name', data.name);
  console.log('password', data.password);
})


  [1]: https://i.stack.imgur.com/uYA9i.png
viren
  • 44
  • 5