我有json对象,我想找到具有父级索引值的id

时间:2018-06-28 13:45:09

标签: javascript arrays angular typescript object

很抱歉,如果您难以理解。

我有一个对象,我想找到subSiteId,其值等于11909,并且我还想要带有父对象的对象索引值的结果。

示例1:在我的情况下,我的subSiteId == 11909在items属性索引中的位置为1,因此我的结果应类似于1_0

示例2:在我的情况下,我的subSiteId == 11999在items属性索引3中的位置,所以我的结果应该像这样3_1

 let obj =[
{
    "subSiteName": "7391 - Belt Line",
    "subSiteId": 1132,
    "SiteId": 1132,
    "items": [
        {
            "subSiteId": 11906,
            "parentId": 0,
            "subSiteName": "Sub Sites 1",
            "hasChildren": true,
            "items": []
        },
        {
            "subSiteId": 11908,
            "parentId": 0,
            "subSiteName": "Sub Sites 2",
            "hasChildren": true,
            "items": [
                {
                    "subSiteId": 11909,
                    "parentId": 11908,
                    "subSiteName": "Sub Sites 3",
                    "hasChildren": false
                }
            ],
            "isVisited": true
        },
        {
            "subSiteId": 11910,
            "parentId": 0,
            "subSiteName": "Sub Sites 4",
            "hasChildren": true,
            "items": []
        },
        {
            "subSiteId": 11914,
            "parentId": 0,
            "subSiteName": "Building",
            "hasChildren": false,
            "items": [
                {
                    "subSiteId": 11200,
                    "parentId": 11908,
                    "subSiteName": "Sub Sites 14",
                    "hasChildren": false
                },
                {
                    "subSiteId": 11999,
                    "parentId": 11908,
                    "subSiteName": "Sub Sites 14",
                    "hasChildren": false
                }
            ]
        }
    ]
}
]

我尝试使用代码:

    subSiteActive(obj, id){
      let itemIndex:string = '0';

     if(obj.items){
    //while(result > 0){
    for(let i = 0; i < obj.items.length; i++){
      if(obj.items[i].subSiteId === id){
        console.log(obj.items, i)
          itemIndex+= '_'+i;
          this.result = true;
          break;
        }else{
          if(!this.result){
            this.subSiteActive(obj.items[i], id)
          }
        }
      }
    return itemIndex;
    //}
    } 
   }
   this.subSiteActive(obj, 11909)

1 个答案:

答案 0 :(得分:0)

尝试一下

 let obj =[
{
    "subSiteName": "7391 - Belt Line",
    "subSiteId": 1132,
    "SiteId": 1132,
    "items": [
        {
            "subSiteId": 11906,
            "parentId": 0,
            "subSiteName": "Sub Sites 1",
            "hasChildren": true,
            "items": []
        },
        {
            "subSiteId": 11908,
            "parentId": 0,
            "subSiteName": "Sub Sites 2",
            "hasChildren": true,
            "items": [
                {
                    "subSiteId": 11909,
                    "parentId": 11908,
                    "subSiteName": "Sub Sites 3",
                    "hasChildren": false
                }
            ],
            "isVisited": true
        },
        {
            "subSiteId": 11910,
            "parentId": 0,
            "subSiteName": "Sub Sites 4",
            "hasChildren": true,
            "items": []
        },
        {
            "subSiteId": 11914,
            "parentId": 0,
            "subSiteName": "Building",
            "hasChildren": false,
            "items": [
                {
                    "subSiteId": 11200,
                    "parentId": 11908,
                    "subSiteName": "Sub Sites 14",
                    "hasChildren": false
                },
                {
                    "subSiteId": 11999,
                    "parentId": 11908,
                    "subSiteName": "Sub Sites 14",
                    "hasChildren": false
                }
            ]
        }
    ]
}
]

var result={};

obj.map(function(ob){
    var f;
	ob.items.forEach(function(item, index){
    item.items.some(function(it, i){
      f =i;
      return item.subSiteId == 11999;
    });
    result = {
        pIndex: index,
        cIndex: f
    };
  });
});
console.log(result);