Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform an object with objects in it

I have the following key/value object

form: {
  id: {value: this.item.id, hidden: true},
  title: {value: this.item.title},
  translations: {
    en: {
      locale: {value: 'en', hidden: true},
      name: {value: 'Hello World'}
    },
    nl: {
      locale: {value: 'nl', hidden: true},
      name: {value: 'Hallo Wereld'}
    }
  }
}

So each key has an object with a value and a hidden attribute, I'm stuck on how to achieve the following. Transform the nested object so each key just has it's value. Not the object with value and hidden. The big issue it seems for me is that it's nested.. So it has to work recursive.

This is the desired end result

form: {
  id: this.item.id,
  title: this.item.title,
  translations: {
    en: {
      locale: 'en',
      name: 'Hello World'
    },
    nl: {
      locale: 'nl',
      name: 'Hallo Wereld'
    }
  }
}

I've tried

Using a combination of Object.keys(form).map(...), which gives me the key of each item, but that's not going to work recursive I'm afraid.

like image 359
Miguel Stevens Avatar asked Mar 12 '19 12:03

Miguel Stevens


People also ask

How do you turn an object into an array of objects?

Use the Object. values() method to convert an object to an array of objects, e.g. const arr = Object. values(obj) .

How a transformation is applied on an object?

Use the Transformations docker in CorelDRAW to position, rotate, scale, mirror, and skew objects with ease and precision. You can transform selected objects, and you can transform copies of the selected objects, leaving the original objects unchanged.

Can I use object entries ()?

Object. entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually.

Which method is used to convert nested arrays to object?

toString() method is used to convert: an array of numbers, strings, mixed arrays, arrays of objects, and nested arrays into strings.

How do you transform an object without transforming its frame?

Select an object to transform. To transform both the frame and its content, use the Selection tool to select the frame. To transform the content without transforming its frame, use the Direct Selection tool to direct-select the object, or use the Selection tool to click the content grabber of an image.

How do you transform an object with a pattern?

Other methods of transforming (such as using the Scale tool) use the object’s center point or the pointer location as the reference point. When you move, rotate, reflect (flip), scale, or shear an object that is filled with a pattern, you can transform just the object, just the pattern, or both the object and pattern.

How do you transform an object in illustrator?

Applies to: Adobe Illustrator Adobe Illustrator. Transforming encompasses moving, rotating, reflecting, scaling, and shearing objects. You can transform objects using the Transform panel, Object > Transform commands, and specialized tools.

How do I transform the content of an object?

To transform both the frame and its content, use the Selection tool to select the frame. To transform the content without transforming its frame, use the Direct Selection tool to direct-select the object, or use the Selection tool to click the content grabber of an image.


1 Answers

function transform(obj) {
  return Object.entries(obj).reduce((newObj, [name, value]) => ({ ...newObj, [name]: value.value === undefined ? transform(value) : value.value  }), {})
}

const form = {
  id: {value: '77777', hidden: true},
  title: {value: '11111'},
  translations: {
    en: {
      locale: {value: 'en', hidden: true},
      name: {value: 'Hello World'}
    },
    nl: {
      locale: {value: 'nl', hidden: true},
      name: {value: 'Hallo Wereld'}
    }
  }
}

console.log(transform(form))

Explanation:

function transform(obj) {
  const entries = Object.entries(obj) // transform object to Array<[propertyName, propertyValue]>

  const tranformedObject = entries.reduce(reducer, {}) // inital value for the first arg of reducer is {}

  return tranformedObject
}

function reducer(newObj, [propertyName, propertyValue]) { // name 
  return {
    ...newObj, // get all properties (that we already set) from prev newObj
    [propertyName]: propertyValue.value === undefined ? transform(propertyValue) : propertyValue.value // if property has .value use it or use recursively tranformed object
  }  // returned value will be set to newObj, and than returned to tranformedObject
}

{ ...prop, [name]: vaue } - it's ES6 syntax

like image 142
AlexOwl Avatar answered Oct 22 '22 09:10

AlexOwl