如何使用Angular.js / Javascript使用子数组键值获取父对象值

时间:2022-04-30 11:21:28

I need one help. I need to retrieve the parent object value if any child key value is there using Angular.js or Javascript. I am explaining my code below.

我需要一个帮助。如果使用Angular.js或Javascript有任何子键值,我需要检索父对象值。我在下面解释我的代码。

$scope.data = [{
    "parentdes": "Parent description1",
    "parent_id":"1"
    "childdes": [{
      "des": 'chile description11',
      "sub_id":"11"
      "subchilddes": [{
        "des": 'subchild des111',
        "sub_sub_id":"111"
      }]
    }, {
      "des": 'chile description12',
      "sub_id":"12"
      "subchilddes": [{
        "des": 'subchild des112',
         "sub_sub_id":"112"
      }]
    }]
  }, {
    "parentdes": "Parent description2",
    "parent_id":"2"
    "childdes": [{
      "des": 'chile description21',
      "sub_id":"21"
      "subchilddes": [{
        "des": 'subchild des212',
        "sub_sub_id":"212"
      }]
    }, {
      "des": 'chile description22',
      "sub_id":"22"
      "subchilddes": [{
        "des": 'subchild des222',
        "sub_sub_id":"222"
      }]
    }]
  }];

Here I have one abject which has value like parent->child->subchild means there are three level. Here suppose I have the id sub_sub_id:222 in this case I need to fetch its parent obeject value i.e- $scope.data[1]['childdes'][1] and also parent to parent object value i.e-$scope.data[1].Here I need both the parent object index and by using that index the object value using Angular.js/Javascript. Please help.

在这里,我有一个具有像parent-> child-> subchild这样的值的abject意味着有三个级别。这里假设我有id sub_sub_id:222在这种情况下我需要获取其父obeject值ie- $ scope.data [1] ['childdes'] [1]以及父对象值ie- $ scope.data [1]。我需要父对象索引和使用该索引使用Angular.js / Javascript的对象值。请帮忙。

1 个解决方案

#1


0  

You'll probably need to write a function which starts to check from the top for it's child elements. If the structure of your data does not change, i.e. if $scope.data is always an array, similarly, if subchilddes is also an array, then your code could be straight forward.

您可能需要编写一个函数,从顶部开始检查它的子元素。如果数据的结构没有改变,即$ scope.data总是一个数组,同样,如果子子节点也是一个数组,那么你的代码可能是直截了当的。

Otherwise, you'll have to see whether the next element is object or array.

否则,您将必须查看下一个元素是对象还是数组。

Your code could go like this.

你的代码可能是这样的。

  1. start from $scope.data
  2. 从$ scope.data开始
  3. if $scope.data is an array,
  4. 如果$ scope.data是一个数组,
  5. then for each element of array,
  6. 那么对于数组的每个元素,
  7. do, check element,
  8. 做,检查元素,
  9. isString? isArray? isObject
  10. isString? IsArray的?则IsObject
  11. if isString
  12. 如果是isString
  13. if element's value is equal to your value,
  14. 如果元素的值等于你的值,
  15. print the trace (you'll have to save this in earlier steps).
  16. 打印跟踪(您必须在前面的步骤中保存它)。
  17. else, if not isString, go back to step 2.
  18. 否则,如果不是isString,请返回步骤2。

If you can come up with some code, I might be able to help you.

如果您能提出一些代码,我可以帮助您。

#1


0  

You'll probably need to write a function which starts to check from the top for it's child elements. If the structure of your data does not change, i.e. if $scope.data is always an array, similarly, if subchilddes is also an array, then your code could be straight forward.

您可能需要编写一个函数,从顶部开始检查它的子元素。如果数据的结构没有改变,即$ scope.data总是一个数组,同样,如果子子节点也是一个数组,那么你的代码可能是直截了当的。

Otherwise, you'll have to see whether the next element is object or array.

否则,您将必须查看下一个元素是对象还是数组。

Your code could go like this.

你的代码可能是这样的。

  1. start from $scope.data
  2. 从$ scope.data开始
  3. if $scope.data is an array,
  4. 如果$ scope.data是一个数组,
  5. then for each element of array,
  6. 那么对于数组的每个元素,
  7. do, check element,
  8. 做,检查元素,
  9. isString? isArray? isObject
  10. isString? IsArray的?则IsObject
  11. if isString
  12. 如果是isString
  13. if element's value is equal to your value,
  14. 如果元素的值等于你的值,
  15. print the trace (you'll have to save this in earlier steps).
  16. 打印跟踪(您必须在前面的步骤中保存它)。
  17. else, if not isString, go back to step 2.
  18. 否则,如果不是isString,请返回步骤2。

If you can come up with some code, I might be able to help you.

如果您能提出一些代码,我可以帮助您。