如何在javascript中過濾嵌套對象屬性並打印新對象

羅姆卡

我得到了這樣構建的對象,它是動態的,所以我不知道有多少嵌套對象和外部對象鍵。外面唯一已知的鍵是“bids”,裡面是(需要的屬性)。

let data= {
  "/9968336/header-bid-tag-0": {
    "bids": [
      {
        "bidderCode": "appnexus",
        "width": 300,
        "height": 250,
        "statusMessage": "Bid available",
        "adId": "7a53a9d3",
        "creative_id": 29681110,
        "cpm": 0.5,
        "adUrl": "https://nym1.ib.adnxs.com/ab?e=wqT_3QLzBKBqAgAAAgDWAAUIkav6sAUQucfc0v-nzQcYj…r=http%3A%2F%2Flocal%3A4000%2Fexamples%2Fpbjs_partial_refresh_example.html",
        "requestTimestamp": 1444844944095,
        "responseTimestamp": 1444844944180,
        "timeToRespond": 85,
        "adUnitCode": "/19968336/header-bid-tag-0",
        "bidder": "appnexus",
        "usesGenericKeys": true,
        "size": "300x250",
        "adserverTargeting": {
          "hb_bidder": "appnexus",
          "hb_adid": "7a53a9d3",
          "hb_pb": "0.50"
        }
      },{
        "bidderCode": "pubmatic",
        "width": "300",
        "height": "250",
        "statusMessage": "Bid available",
        "adId": "1139e34e14",
        "adSlot": "39620189@300x250",
        "cpm": 1,
        "ad": "<span class=\"PubAPIAd\"><script src='https://ad.turn.com/server/ads.js?pub=5757398&cch=36757096&code=37127675&l=3…tcGlkPUVERkNGMDY5LTA2ODctNDAxQy04NkMwLTIzQjNFNzI1MzdGNiZwYXNzYmFjaz0w_url='></script></span> <!-- PubMatic Ad Ends -->",
        "adUrl": "https://aktrack.pubmatic.com/AdServer/AdDisplayTrackerServlet?operId=1&pubId…local%3A4000%2Fexamples%2Fpbjs_partial_refresh_example.html&lpu=hotels.com",
        "dealId": "",
        "requestTimestamp": 1444844944105,
        "responseTimestamp": 1444844944354,
        "timeToRespond": 249,
        "adUnitCode": "/19968336/header-bid-tag-0",
        "bidder": "pubmatic",
        "usesGenericKeys": true,
        "size": "300x250",
        "adserverTargeting": {
          "hb_bidder": "pubmatic",
          "hb_adid": "1139e34e14",
          "hb_pb": "1.00"
        }
      },
      {
        "bidderCode": "rubicon",
        "width": "300",
        "height": "250",
        "statusMessage": "Bid available",
        "adId": "130d3b0d9b",
        "cpm": 0.795995,
        "ad": "<scri...pt>",
        "ad_id": "3161645",
        "sizeId": "15",
        "requestTimestamp": 1444844944116,
        "responseTimestamp": 1444844944396,
        "timeToRespond": 280,
        "adUnitCode": "/19968336/header-bid-tag-0",
        "bidder": "rubicon",
        "usesGenericKeys": true,
        "size": "300x250",
        "adserverTargeting": {
          "hb_bidder": "rubicon",
          "hb_adid": "130d3b0d9b",
          "hb_pb": "0.50"
        }
      }
    ]
  },
  "/9968336/header-bid-tag1": {
    "bids": [
      {
        "bidderCode": "casale",
        "width": 0,
        "height": 0,
        "statusMessage": "Bid returned empty or error response",
        "adId": "108c0ba49d",
        "requestTimestamp": 1444844944130,
        "responseTimestamp": 1444844944223,
        "timeToRespond": 93,
        "cpm": 6,
        "adUnitCode": "/19968336/header-bid-tag1",
        "bidder": "casale"
      },
      {
        "bidderCode": "openx",
        "width": "728",
        "height": "90",
        "statusMessage": "Bid available",
        "adId": "14d7f9208f",
        "ad_id": "537161420",
        "cpm": 1.717,
        "ad": "<iframe src=...tame>",
        "requestTimestamp": 1444844944130,
        "responseTimestamp": 1444844944490,
        "timeToRespond": 360,
        "adUnitCode": "/19968336/header-bid-tag1",
        "bidder": "openx",
        "usesGenericKeys": true,
        "size": "728x90",
        "adserverTargeting": {
          "hb_bidder": "openx",
          "hb_adid": "14d7f9208f",
          "hb_pb": "1.50"
        }
      }
    ]
  }
}
  

我想從內部對像中清除除 (cpm,bidder,adUnitCode) 之外的所有鍵和值,並返回僅具有此屬性的新對象

我在這裡掙扎,我到目前為止要做的事情

 for(let propName in data) {
    if(data.hasOwnProperty(propName)) {
        let propValue = data[propName];
        for(var insideProp in propValue.bids) {
          if(propValue.bids.hasOwnProperty(insideProp)) {
              let insideValue=propValue.bids[insideProp]
              for(let insideLvl2 in insideValue) {
                   if(insideLvl2!=='cpm'){
                     insideValue.insideLvl2=null;
                     console.log(insideValue.insideLvl2);
                   }
              }
               
             }
          
        
        }
    }
}


謝謝你的幫助

解碼器

如果您只想要cpm, bidder, adUnitCode結果對象,那麼您可以輕鬆地使用Object.entriesreducemap

現場演示

代碼沙盒演示

const result = Object.entries(data).reduce((acc, [k, v]) => {
  acc[k] = {
    ...v,
    bids: v.bids.map(({ cpm, bidder, adUnitCode }) => ({
      cpm,
      bidder,
      adUnitCode,
    })),
  };
  return acc;
}, {});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

是否可以通過某些屬性的值過濾嵌套對象?(JS)

Ramda - 如何向嵌套對象添加新屬性

如何更新 MongoDB 中的嵌套對象屬性

如何按值過濾嵌套的對像數組並獲取根對象

如何在 GTK4 中通過對象 ID 應用 CSS 屬性?

如何使用計算屬性和道具過濾對象 [VueJS]

通過嵌套屬性獲取父對象

過濾對象的屬性以僅返回特定類型的屬性

如何在 TypeScript 中使用嵌套對象?“對象”類型上不存在屬性

獲取/過濾與 Python 中的屬性/鍵匹配的對象列表中的第一項

如何返迴響應對象的屬性以及新對象?

過濾對象列表

如何通過相同的屬性 unsing LINQ 組合集合中的對象?

休眠搜索:使用過濾器對嵌套對象進行排序,如何?

過濾複雜對像中的數組數組並返回該對象

如何使用javascript將對象屬性解析為嵌套數組數組?

Javascript中的數組到屬性對象

僅當該屬性存在時,如何更新 mongo 中的嵌套對象?

如何訪問作為python中類屬性的對象列表中的對象屬性?

如何向 Mongoose 中 findByID 的對象添加新屬性?

如何根據鍵過濾對象的數據並使新數組做出反應?

如何在打字稿/ javascript中將異步常量分配給對象的屬性

帶有要過濾的嵌套數組的對象,想要返回帶有新過濾數組的整個對象

如何在貓鼬中更新數組中對象的屬性

如何將新對象插入對象並解開這個新對象?

Powershell:對像中的對象 - 提取屬性值(並導出到 CSV) - OneNoteUtilitiesGraph

如果該對象包含在 javascript 中的對像數組中,如何檢查整個原始對象(不使用任何屬性)?

如何在reactjs中映射對象的按順序編號的屬性

如何在 JLabel 中顯示對象的屬性?(爪哇)