【问题标题】:How to get particular data values from JSON Node.js?如何从 JSON Node.js 获取特定的数据值?
【发布时间】:2021-10-10 04:56:46
【问题描述】:

这是我的 JSON 文件输出:

let employees = [{
  "id":1,
  "name":"Lann",
  "username":"brot",
  "email":"b@sd.com",
  "address": {
    "city":"Gweh",
    "zipcode":"92998-3874",
    "geo": {
      "lat":"45",
      "lng":"77"
    }
  }
}]

我如何从下面获取 ID、姓名和电子邮件:

{
  "id":1,
  "name":"Lann",
  "email":"b@sd.com"
}

【问题讨论】:

  • 嗨!您的 json 输入格式错误:缺少 },即 }] 应该是 }}] 如果输入正确,您可以 console.log(employees[0].name); 例如。

标签: javascript node.js json


【解决方案1】:

如果您的数组只有一个元素,您可以只访问信息,无需像这样构建另一个数组:employees[0].idemployees[0].nameemployees[0].email,或者您可以使用对象解构提取一个对象

let employees = [{
  "id": 1,
  "name": "Lann",
  "username": "brot",
  "email": "b@sd.com",
  "address": {
    "city": "Gweh",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "45",
      "lng": "77"
    }
  }
}];
const picked = (({ id, name, email }) => ({  id, name, email }))(employees[0]);
console.log(picked);

但是如果你的数组有更多的员工,我认为你需要做的是通过 id 或 name 搜索并返回一个信息最少的对象,你可以这样做

let employees = [{
  "id": 1,
  "name": "Lann",
  "username": "brot",
  "email": "b@sd.com",
  "address": {
    "city": "Gweh",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "45",
      "lng": "77"
    }
  }
}];
let employee = employees.find(o => o.name === 'Lann');
let picked = (({ id, name,email }) => ({ id, name,email }))(employee);
console.log(picked);

【讨论】:

    【解决方案2】:

    您可以使用地图存档。

    let employees = [{
              "id":1,
              "name":"Lann",
              "username":"brot",
              "email":"b@sd.com",
              "address":{
                 "city":"Gweh",
                 "zipcode":"92998-3874",
                 "geo":{
                    "lat":"45",
                    "lng":"77"
                 }
              }
              }]
    const data = employees.map(o => ({ id: o.id, name: o.name, email:o.email }));
    console.log(data[0]);

    【讨论】:

      【解决方案3】:

      您可以通过使用数组解构来简单地做到这一点。

      let employees = [{
                "id":1,
                "name":"Lann",
                "username":"brot",
                "email":"b@sd.com",
                "address":{
                   "city":"Gweh",
                   "zipcode":"92998-3874",
                   "geo":{
                      "lat":"45",
                      "lng":"77"
                   }
                }}];
      
      // Destructuring array
      const [employee] = employees;
      

      ** 现在从这里employee 是一个对象,您可以像访问其他对象一样正常访问它的属性。获取id,name,username:**

      employee.id;
      employee.name;
      employee.username;
      

      【讨论】:

        【解决方案4】:

        如果它包含多个项目,您还可以循环通过您的输入并获得一组收缩项目:

        let employees = [{
          "id": 1,
          "name": "Lann",
          "username": "brot",
          "email": "b@sd.com",
          "address": {
            "city": "Gweh",
            "zipcode": "92998-3874",
            "geo": {
              "lat": "45",
              "lng": "77"
            }
          }
        }]
        
        let shrink = [];
        
        for (let employee of employees) {
          shrink.push({
            id: employee.id,
            name: employee.name,
            email: employee.email
          });
        }
        
        console.log(shrink);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          • 2019-03-30
          • 2023-03-04
          • 1970-01-01
          • 1970-01-01
          • 2017-09-19
          相关资源
          最近更新 更多