根据parameterName获取parameterDescription

时间:2018-11-30 18:14:35

标签: javascript angularjs

如何基于angularjs中的parameterDescription从下面的对象中获取parameterName

{
  "_embedded" : {
    "parameterMasters" : [ {
      "id" : "MUTED_EMAIL_SHOW_PARAMETER",
      "resourceName" : "SHOW_PARAMETER",
      "parameterName" : "muted_email",
      "parameterDescription" : "Email Id",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/eventiq/api"
        }
      }
    }, {
      "id" : "ALLOWED_TIMESLOTS_SHOW_PARAMETER",
      "resourceName" : "SHOW_PARAMETER",
      "parameterName" : "allowed_timeslots",
      "parameterDescription" : "Allowed Timeslots",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/eventiq/api"
        }
      }
    }, {
      "id" : "ROOM_BLOCKING_INTERVAL_SHOW_PARAMETER",
      "resourceName" : "SHOW_PARAMETER",
      "parameterName" : "room_blocking_interval",
      "parameterDescription" : "Room Blocking Interval",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/eventiq/api"
        }
      }
    }, {
      "id" : "MIN_SLOT_LENGTH_SHOW_PARAMETER",
      "resourceName" : "SHOW_PARAMETER",
      "parameterName" : "min_slot_length",
      "parameterDescription" : "Minimum Slot Length",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/eventiq"
        }
      }
    }, {
      "id" : "LANDING_PAGE_SHOW_PARAMETER",
      "resourceName" : "SHOW_PARAMETER",
      "parameterName" : "landing_page",
      "parameterDescription" : "Landing Page",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/c/api/c/c"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/c/c/c?filter=c"
    }
  },
  "page" : {
    "size" : 50,
    "totalElements" : 5,
    "totalPages" : 1,
    "number" : 0
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.3/angular.min.js"></script>

1 个答案:

答案 0 :(得分:1)

您可以创建一个函数,该函数将使用该对象和值parameterName,在数组内部找到相应的对象,然后返回找到的对象的parametedDescription

function findByParameterName(o, paramName) {
    return o._embedded.parameterMasters.find(
        entry => entry.parameterName === paramName
    ).parameterDescription;
}

const o = {
    _embedded: {
        parameterMasters: [
            {
                id: 'MUTED_EMAIL_SHOW_PARAMETER',
                resourceName: 'SHOW_PARAMETER',
                parameterName: 'muted_email',
                parameterDescription: 'Email Id',
                _links: {
                    self: {
                        href: 'http://localhost:8080/eventiq/api'
                    }
                }
            },
            {
                id: 'ALLOWED_TIMESLOTS_SHOW_PARAMETER',
                resourceName: 'SHOW_PARAMETER',
                parameterName: 'allowed_timeslots',
                parameterDescription: 'Allowed Timeslots',
                _links: {
                    self: {
                        href: 'http://localhost:8080/eventiq/api'
                    }
                }
            },
            {
                id: 'ROOM_BLOCKING_INTERVAL_SHOW_PARAMETER',
                resourceName: 'SHOW_PARAMETER',
                parameterName: 'room_blocking_interval',
                parameterDescription: 'Room Blocking Interval',
                _links: {
                    self: {
                        href: 'http://localhost:8080/eventiq/api'
                    }
                }
            },
            {
                id: 'MIN_SLOT_LENGTH_SHOW_PARAMETER',
                resourceName: 'SHOW_PARAMETER',
                parameterName: 'min_slot_length',
                parameterDescription: 'Minimum Slot Length',
                _links: {
                    self: {
                        href: 'http://localhost:8080/eventiq'
                    }
                }
            },
            {
                id: 'LANDING_PAGE_SHOW_PARAMETER',
                resourceName: 'SHOW_PARAMETER',
                parameterName: 'landing_page',
                parameterDescription: 'Landing Page',
                _links: {
                    self: {
                        href: 'http://localhost:8080/c/api/c/c'
                    }
                }
            }
        ]
    },
    _links: {
        self: {
            href: 'http://localhost:8080/c/c/c?filter=c'
        }
    },
    page: {
        size: 50,
        totalElements: 5,
        totalPages: 1,
        number: 0
    }
};

function findByParameterName(o, paramName) {
    return o._embedded.parameterMasters.find(
        entry => entry.parameterName === paramName
    ).parameterDescription;
}

console.log(findByParameterName(o, 'allowed_timeslots'));