2

I got object that built like this,it is dynamicly so i dont't know how many nested objects will be and the outside object keys .The only known keys outside is "bids",and inside is (needed properties).

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"
        }
      }
    ]
  }
}
  

and I want to clear from the inside object all the keys and value except (cpm,bidder,adUnitCode) and return the new object with only this properties

I am strurgle here that what I tray to do so far

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


thanx for help

0

1 Answer 1

2

If you only want cpm, bidder, adUnitCode in the resultant object then you can easily do using Object.entries, reduce, and map

Live Demo

Codesandbox Demo

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

EDITED: If you want to return cpm=null if the is no cpm property in the object itself then you can do as:

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

let data = {
  "/9968336/header-bid-tag1": {
    bids: [
      {
        bidderCode: "casale",
        statusMessage: "Bid returned empty or error response",
        adId: "108c0ba49d",
        requestTimestamp: 1444844944130,
        responseTimestamp: 1444844944223,
        timeToRespond: 93,
        adUnitCode: "/19968336/header-bid-tag1",
        bidder: "casale",
      },
    ],
  },
};

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

console.log(result);

Sign up to request clarification or add additional context in comments.

5 Comments

thanx for this it is good but , how I makes sure that values in the inside object present and make if statment for example :if(cpm not found do x )
tell me the whole scenario with an example and what should be your expected output?
the inside object(last lvl is dynamic,so in some cases some of the parameters can not exisst for example :` let data= { "/9968336/header-bid-tag1": { "bids": [ { "bidderCode": "casale", "statusMessage": "Bid returned empty or error response", "adId": "108c0ba49d", "requestTimestamp": 1444844944130, "responseTimestamp": 1444844944223, "timeToRespond": 93, "adUnitCode": "/19968336/header-bid-tag1", "bidder": "casale" }` in this case I want return same object but cpm=null @decpk
@Romka edited code, have a look
thanx it working well :>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.