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'
}
}
}
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.
Use the Object. values() method to convert an object to an array of objects, e.g. const arr = Object. values(obj) .
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.
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.
toString() method is used to convert: an array of numbers, strings, mixed arrays, arrays of objects, and nested arrays into strings.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With