O
O
ojiekcahdp2021-03-01 11:26:53
JavaScript
ojiekcahdp, 2021-03-01 11:26:53

How to get values ​​from objects?

There is a JSON like:

[
 {
  "name": "Anton",
  "lastname": "Romanov"
   "rank": "admin"
 },
 {
  "name" "Andrey",
  "lastname": "Ivanov"
  "rank":  "admin"
 },
 {
  "name": "Vasya",
  "lastname": "Antonov"
   "rank": "admin"
 },
 {
  "name" "Ivan",
  "lastname": "Andreev"
  "rank":  "admin"
 }
 {
  "name" "Ivan",
  "lastname": "Vasiliv"
  "rank":  "player"
 }
]


How can I get information about all values ​​of rank: admin, and use, for example:
console.log(`Admins: ${admins}`) - lists name and lastname of all admins separated by commas.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Gololobov, 2021-03-01
@ojiekcahdp

const users = [ .......  ]
const admins = users.filter(user => user.rank === 'admin').map(user => `${user.lastname} ${user.name}`).join(', ');
console.log(`Админы: ${admins}`);

R
Rsa97, 2021-03-01
@Rsa97

JSON.parse() , Array.filter() , Array.map() , Array.join()

T
Tigran Abrahamyan, 2021-03-01
@TAbrahamyan

const admins = data
  .filter(n => n.rank === 'admin')
  .reduce((acc, n) => {
    acc += `${n.name} ${n.lastname}, `;
    return acc;
  }, '').trim().replace(/,$/, '.');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question