alexa
Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

How to generically replace every undefined value within a tree like object structure with the null value in JS ?

How to generically replace every undefined value within a tree like object structure with the null value in JS ?

I think what would be most useful is a function that takes an output object specification describing where its elements are to be found in the original structure. It's simple enough to write recursively. This version allows you to specify a property of the input for any location in the output, passing null for those that are missing:

 

 const restructure = (spec) => (o) =>
  Object .fromEntries (Object .entries (spec) .map (([k, v]) => [
    k,
    Object (v) === v ? restructure (v) (o) : v in o ? o [v] : null
  ]))


const spec = {
   bio: 'bio',  
   fullname: 'fullname',
   company: 'company',
   position: 'position',
   profilImg: 'item',
   talentType: 'talentType',
   rating: 'rating',
   contacts: {
     gmeets: 'gmeet',
     zoom: 'zoom',
     phone: 'phone',
   },
   skills: 'skills',
   email: 'email',
   firstname: 'firstname',
   lastname: 'lastname',
   phone: 'phoneNum',
   ratingCount: 'ratingCount',
   sumRatings: 'sumRatings',
   currentPosition: 'currentPosition',
   creationTime: 'creationTime',
   rgpd: 'rgpd',
   emptyComs: 'emptyComs',
}


const o = {sumRatings: "Private info", phoneNum: "Private info", firstname: "Private info", email: "Private info", fullname: "Private info", talentType: "Private info", ratingCount: "Private info", creationTime: "Private info", lastname: "Private info", currentPosition: "Private info", rgpd: "Private info", bio: "Private info", company: "Private info", whatsapp: "Private info", emptyComs: "Private info", id: "Private info"}

console .log (restructure (spec) (o))
 .as-console-wrapper {max-height: 100% !important; top: 0}

39 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online