bugfix> javascript > 投稿

分かりにくい場合は申し訳ありません。

オブジェクトがあり、値が11909に等しいsubSiteIdを見つけたいです。また、親のオブジェクトインデックス値の結果も必要です。

例1:私の場合、subSiteId == 11909のアイテムプロパティインデックス1内の位置なので、結果は次のようになります1_0

例2:私の場合、subSiteId == 11999のアイテムプロパティインデックス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 件
  • これを試して

    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);
    
    

あなたの答え