Home > Mobile >  Apply child value to parent key if specific key exists in child in object
Apply child value to parent key if specific key exists in child in object

Time:11-21

const obj = {
  uid: "893212",
  a: {name: "Down here!", uid: "1231"},
  b: {
    c: {uid: "5965"},
    name: "bud name",
  }, 
  d: {name: "doodle name"},
  e: {name: "alexa name"},
  f: ["kk", "jj"],
  g: [
   {
     h: {uid: "47895"},
     i: {uid: "4785"}
   },
   {
     j: {uid: "4895"}
   }
  ]
};

in the above object if "uid" exist its value should be applied to its parent as a value. result should be as below

var result = {
  uid: "893212",
  a: "1231",
  b: {c: "5965", name: "bud name"},
  d: {name: "doodle name"},
  e: {name: "alexa name"},
  f: ["kk", "jj"],
  g: [
   {h: "47895", i: "4795"},
   {j: "4895"}
  ]
}

I tried to use recursive function to manipulate object.

const mapObj = (obj = {}) => {
  if (isObject(obj)) {
    const entries = Object.entries(obj);

    for (let i = 0; i < entries.length; i  = 1) {
      const [objK, objV] = entries[i];
      
      if (isObject(objV) && 'uid' in objV) {
        obj[objK] = objV['uid'];          
      } else if(isObject(objV)){
        findNestedObject(objV);
      } else if(isArray(objV)) {
        objV.forEach(val => {
          findNestedObject(val);
        })
      }
    } 
  }
};

is there any simple way to do it and array of objects are converted

CodePudding user response:

Try this:

const obj = {
  uid: "893212",
  a: {
    name: "Down here!",
    uid: "1231",
  },
  b: {
    c: {
     uid: "5965",
     },
     name: "bud name",
  }, 
  d: {
      name: "doodle name"
  },
  e: {
      name: "alexa name"
  },
  f: ["kk", "jj"],
};

const process = (anObject) => {
  const toReturn = {};
  Object.keys(anObject).forEach((key, index) => {
    const currentProperty = anObject[key];  
    if(Array.isArray(currentProperty)) {  
      toReturn[key] = currentProperty;
    }
    else if(typeof(currentProperty) === 'object') {
      const uid = currentProperty?.uid;
      if(uid) {
        toReturn[key] = uid;
      } 
      else {
        toReturn[key] = process(currentProperty);
      }         
    } else {
      toReturn[key] = currentProperty;
    }
  });
  return toReturn;
}

const target = process(obj);
console.log(JSON.stringify(target, null, 2));

CodePudding user response:

Try it:

const obj = { uid: "893212", a: { name: "Down here!", uid: "1231", }, b: { c: { uid: "5965", }, name: "bud name", }, d: { name: "doodle name" }, e: { name: "alexa name" }, f: ["kk", "jj"]};

const refactor = (ins, depth) => {
  if (Array.isArray(ins)) {
    return ins.map(it => refactor(it, depth 1));
  }
  
  if (typeof ins === "object") {
    if (ins["uid"] && depth !== 0) return ins.uid;
    return Object.entries(ins).reduce((a, [k, v]) => ({
      ...a,
      [k]: refactor(v, depth 1)
    }), {});
  }
  return ins;
}

console.log(refactor(obj, 0));

CodePudding user response:

A recursion passing a pointer to a parent when calling it on children.

const obj = {uid:"893212",a:{name:"Down here!",uid:"1231"},b:{c:{uid:"5965"},name:"bud name"},d:{name:"doodle name"},e:{name:"alexa name"},f:["kk","jj"]};

function do_obj(obj, parent, parent_key) {
  Object.keys(obj).forEach(function(key) {
    var item = obj[key];
    if (key === 'uid' && parent) {
      parent[parent_key] = item;
    }
    if (typeof item === 'object' && item !== null) {
      do_obj(item, obj, key);
    }
  })
}

do_obj(obj)
console.log(obj)
.as-console-wrapper {
  max-height: 100% !important
}

  • Related