按照Hoektransform()
文档上的示例,有没有办法在源对象中转换数组?
将示例转换为带有数组的内容,如下所示:
var source = {
address: [
{
one: '123 main street',
two: 'PO Box 1234'
},
{
one: '456 fake street',
two: 'Apt 2b'
}],
title: 'Warehouse',
state: 'CA'
};
var result = Hoek.transform(source, {
'person.address.lineOne': 'address.one',
'person.address.lineTwo': 'address.two',
'title': 'title',
'person.address.region': 'state'
});
结果是:
{
person: {
address: {
lineOne: undefined,
lineTwo: undefined,
region: 'CA'
}
},
title: 'Warehouse'
}
编辑:添加预期结果:
{
person: {
address: [{
lineOne: '123 main street',
lineTwo: 'PO Box 1234',
region: 'CA'
},
{
lineOne: '465 fake street',
lineTwo: 'Apt 2b',
region: 'CA'
}]
},
title: 'Warehouse'
}
我认为我要完成的工作超出了此方法的范围,但我想确定这不是用户错误。
如果它超出了 hoek 的转换范围,有什么建议可以将 JS 对象映射到具有新键名的新对象的好方法吗?