Convert an object with one property to a parent property

I converted the javascript object from xml, this is an example object:

{
name: 'current name',
  attr1: 'attribute1',
  attr2: 'attribute2',
  address: {
    name: 'name1',
    value: {
      value: '12'
    },
    attr3: {
      name: 'no name',
      attr4: {
        attr4: 'attribute4'
      }
    }
  },
  price: {
    price: '500'  
  },
  in_house: {
    in_house: '2'
  }
}

how can i convert to this:

{
name: 'current name',
  attr1: 'attr1',
  address:{
    name: 'name1',
    value: '12',
    attr3: {
      name: 'no name',
      attr4: 'attribute3'
    }
  }
  attr2: 'attr2',
  price: 500,
  in_house: 2
}

you need to convert the entire unused object to a property, for example {price: price: '500'} to {price: '500'}

+4
source share
3 answers

You can use an iterative, recursive approach for keys and their values.

function moveUp(object, last) {
    var keys = Object.keys(object);

    if (keys.length === 1 && keys[0] in last) {
        last[keys[0]] = object[keys[0]];
        if (last[keys[0]] !== null && typeof last[keys[0]] === 'object') {
            moveUp(last[keys[0]], last);
        }
        return;
    }
    keys.forEach(function (k) {
        if (object[k] !== null && typeof object[k] === 'object') {
            moveUp(object[k], object)
        }
    });
}

var object = { name: 'current name', attr1: 'attribute1', attr2: 'attribute2', address: { name: 'name1', value: { value: '12' }, attr3: { name: 'no name', attr4: { attr4: 'attribute4' } } }, price: { price: '500' }, in_house: { in_house: '2' }, test: { test: { test: { banane: 42 } } } };

moveUp(object);

console.log(object);	
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run codeHide result
+4
source

Here is a recursive function that will iterate over the root object and pass all the node in it to see if the current node has an immediate child with the same name.

const obj = { name: 'current name', attr1: 'attribute1', attr2: 'attribute2',
  address: { name: 'name1', value: { value: '12' }, attr3: { name: 'no name', attr4: { attr4: 'attribute4' }}}, price: { price: '500' }, in_house: { in_house: '2' }}
// helper function to check if a value is an object
const isObject = thing => (
  typeof thing !== 'undefined' && 
  typeof thing.constructor && 
  thing.constructor === Object
)

const mutateUselessProperties = (root) => {
  // we need to recursively go through the root object and return it result
  // after removing properties so we create an inner function for recursion
  const go = (obj) => {
    // if it just a value return it
    if (!isObject(obj)){
      return obj
    }
    // it an object so we loop over the keys
    for (let key in obj) {
      // check if it an object with a child of the same key
      if (isObject(obj[key]) && obj[key][key]) {
        // reassign the property to it child of the same name
        obj[key] = obj[key][key]
      }
      // check if it still an object after possible reassignment
      if (isObject(obj[key])) {
        // it an object so recrusively go through the child properties
        obj[key] = go(obj[key])    
      }
      // may as well check if we are dealing with an array at the same time
      if (Array.isArray(obj[key])) {
        obj[key] = obj[key].map(go)    
      }
    }
    // return the current iteration
    return obj
  }
  // run the recursive iteration
  go(root)
  // return the root object that has been mutated
  return root
}

console.log(mutateUselessProperties(obj))
Run codeHide result
+1
source

, :

var obj = {
name: 'current name',
  attr1: 'attribute1',
  attr2: 'attribute2',
  address: {
    name: 'name1',
    value: {
      value: '12'
    },
    attr3: {
      name: 'no name',
      attr4: {
        attr4: 'attribute4'
      }
    }
  },
  price: {
    price: '500'  
  },
  in_house: {
    in_house: '2'
  }
};

for (var prop in obj) typeof obj[prop] === "object" && Object.keys(obj[prop]).length === 1 && (obj[prop] = obj[prop][prop]);
console.log(obj);
Hide result
0

Source: https://habr.com/ru/post/1656443/


All Articles