简体   繁体   English

在JavaScript中获取对象属性

[英]Getting object property in JavaScript

I have a object from a get request from an API like this: 我有一个来自API这样的get请求的对象:

var object = {
  "number": 0,
  "size": 9999,
  "totalPages": 0,
  "numberOfElements": 1,
  "totalElements": 1,
  "content": [{
    "id": 26,
    "name": "New York",
    "acronym": "SP",
    "country": {
      "id": 33,
      "name": "United States",
      "acronym": "US"
    }
  }]
}

If I want to get the country name, I need to do: 如果要获取国家/地区名称,则需要执行以下操作:

object.content[0].country.name or object["content"][0]["country"]["name"]

However I am trying to access trough a ng-repeat those properties that I pass in the controller in order to generalize my controller. 但是,我试图通过ng重复访问我在控制器中传递的那些属性,以便概括我的控制器。

Inside the controller I have: 在控制器内部,我有:

$scope.$parent.fields = ["acronym", "name", "country.name"];
$scope.columns = [
  {
    field: $scope.fields[0],
    "class": 'col-sm-1'
  }, {
    field: $scope.fields[1],
    "class": 'col-sm-5'
  }, {
    field: $scope.fields[2],
    "class": 'col-sm-3'
  }
];

And the HTML: 和HTML:

<tr ng-repeat="item in elements.content">
  <td ng-repeat="column in columns">{{ item[column.field] }}</td>
  <td id="op">
    <button class="btn btn-xs btn-success" ng-click="preview(item)">
      <i class="fa fa-eye"></i> View
    </button>
    <button class="btn btn-xs btn-primary" ng-click="update(item)">
      <i class="fa fa-pencil"></i> Edit
    </button>
    <button class="btn btn-xs btn-danger" ng-click="remove(item)">
      <i class="fa fa-trash"></i> Delete
    </button>
  </td>
</tr>

It only works for the properties from the content object, but not properties of objects inside content. 它仅适用于内容对象的属性,不适用于内容内部对象的属性。 What can I do? 我能做什么?

Thank you in advance for your time and help! 预先感谢您的时间和帮助!

You'll likely need to create a helper function that parses the string you're passing in as a key and returns the object's value because you can't access an object's child properties using a string: 您可能需要创建一个辅助函数,该函数解析作为键传递的字符串并返回对象的值,因为您无法使用字符串访问对象的子属性:

var item = {
  property: {
    child: 'somevalue'
  }
}

// this will return undefined
item['property.child']

Luckily this nice function exists: 幸运的是, 这个不错的功能存在:

function getDescendantProp(obj, desc) {
  var arr = desc.split(".");
  while(arr.length && (obj = obj[arr.shift()]));
  return obj;
}

It takes an object and string, turns the string into an array and traverses the properties of your passed in object until it reaches the property you're searching for. 它接受一个对象和一个字符串,将字符串转换成数组,并遍历传入对象的属性,直到到达要搜索的属性。 It will work for any depth of object property. 它适用于任何深度的对象属性。

if you take this function and put it on your controller's scope you can then call it with your field's name and item object: 如果使用此函数并将其放在控制器的作用域内,则可以使用字段的名称和item对象来调用它:

<td ng-repeat="column in columns">{{ getDescendantProp(item, column.field) }}</td>

Here's a working example of the idea: http://plnkr.co/edit/34fKDKReu24381Ox8kdK?p=preview 这是一个可行的想法示例: http : //plnkr.co/edit/34fKDKReu24381Ox8kdK?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM