【问题标题】:Node.js - Find parameter in array nested in JSON object and return other parametersNode.js - 在嵌套在 JSON 对象中的数组中查找参数并返回其他参数
【发布时间】:2019-08-22 13:58:01
【问题描述】:

我在 Node.js 中有一个 API 调用,它返回一个 JSON 对象。

我需要在这个对象中找到一个客户电话号码(最好通过函数),带有body.result.reservations[X of reserv.].client.phone,并返回参数phonedateFromdateTo,最好是一个数组,带有body.result.reservations[X number].reservationDetails.dateFrom和以此类推。

通过添加点来访问这些参数,但是当我尝试使用互联网上的任何搜索算法时,它会因未定义而崩溃,.phone 不是函数和其他奇特的东西。

这是console.log(body.result.reservations)的输出:

[
    {
        id: 1,
        reservationDetails:
            {
                price: 670,
                dateFrom: '2019-03-29 13:59',
                dateTo: '2019-04-05 19:00',
                status: 'waiting'
            }
    },

    {   id: 2,
        reservationDetails:
            { price: 4500,
                dateFrom: '2019-04-09 20:00',
                dateTo: '2019-05-18 12:00',
                status: 'accepted',
                },
        client:
            {   id: 8,
                login: 'customer@x.com',
                clientType: 'person',
                phone: '+5.500500500',
                } 
     },
     {   id: 4,
        reservationDetails:
            { price: 9500,
                dateFrom: '2020-04-09 13:00',
                dateTo: '2020-04-18 10:00',
                status: 'passed',
                },
        client:
            {   id: 8,
                login: 'customer2@x2.com',
                clientType: 'person',
                phone: '+38.700500500',
                } 
     }
]

【问题讨论】:

    标签: javascript node.js json object nested


    【解决方案1】:

    你可以使用Array.prototype.find:

    const data = [
        {
            id: 1,
            reservationDetails:
            {
                price: 670,
                dateFrom: '2019-03-29 13:59',
                dateTo: '2019-04-05 19:00',
                status: 'waiting'
            }
        },
    
        {
            id: 2,
            reservationDetails:
            {
                price: 4500,
                dateFrom: '2019-04-09 20:00',
                dateTo: '2019-05-18 12:00',
                status: 'accepted',
            },
            client:
            {
                id: 8,
                login: 'customer@x.com',
                clientType: 'person',
                phone: '+5.500500500',
            }
        },
        {
            id: 4,
            reservationDetails:
            {
                price: 9500,
                dateFrom: '2020-04-09 13:00',
                dateTo: '2020-04-18 10:00',
                status: 'passed',
            },
            client:
            {
                id: 8,
                login: 'customer2@x2.com',
                clientType: 'person',
                phone: '+38.700500500',
            }
        }]
        
    function findPhone(phone, data){
      const result = data.find(e => e.client ? e.client.phone === phone : null);
      return result ? result : 'No match found';
    }
    
    console.log(findPhone('+38.700500500', data));
    console.log(findPhone('11111', data));

    这将返回匹配的对象或“未找到匹配”。

    【讨论】:

      【解决方案2】:

      这可能是因为数组中并非所有条目都具有客户端属性,因此没有电话号码。此解决方案为您提供了所有预订的数组,其中电话号码或电话号码作为“丢失的电话号码”,以防丢失。

          const body = {
            result: {
              reservations: [
                {
                  id: 1,
                  reservationDetails:
                  {
                    price: 670,
                    dateFrom: '2019-03-29 13:59',
                    dateTo: '2019-04-05 19:00',
                    status: 'waiting'
                  }
                },
      
                {
                  id: 2,
                  reservationDetails:
                  {
                    price: 4500,
                    dateFrom: '2019-04-09 20:00',
                    dateTo: '2019-05-18 12:00',
                    status: 'accepted',
                  },
                  client:
                  {
                    id: 8,
                    login: 'customer@x.com',
                    clientType: 'person',
                    phone: '+5.500500500',
                  }
                },
                {
                  id: 4,
                  reservationDetails:
                  {
                    price: 9500,
                    dateFrom: '2020-04-09 13:00',
                    dateTo: '2020-04-18 10:00',
                    status: 'passed',
                  },
                  client:
                  {
                    id: 8,
                    login: 'customer2@x2.com',
                    clientType: 'person',
                    phone: '+38.700500500',
                  }
                }]
            }
          } 
      
      
            const reservations = body.result.reservations.map((reservation) => {
              return {
                dateFrom : reservation.reservationDetails.dateFrom,
                dateTo: reservation.reservationDetails.dateTo,
                phone: reservation.client ? reservation.client.phone : 'missing phone number'
              }
             });
      
      

      【讨论】:

      • 你救了我的命,效果很好 - 我只需要添加 .find() 即可找到合适的预订。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 2017-12-24
      相关资源
      最近更新 更多