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 
3
  • Note that the logic for solving this request has nothing to do with AJAX or JSON, so I removed references to them. Commented Feb 26, 2019 at 11:16
  • 1
    Possible duplicate of From an array of objects, extract value of a property as array Commented Feb 26, 2019 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 Commented Feb 26, 2019 at 11:42

4 Answers 4

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]}`);
      }
   }
});
Sign up to request clarification or add additional context in comments.

Comments

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);
})

2 Comments

thank you, it works but when i match the username and password it is not working
What exactly do you mean by 'match the username and password'?
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);

Comments

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.sstatic.net/uYA9i.png

Comments

Your Answer

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.