Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的变异对象 有帮助的注意:考虑阅读“目的是什么”部分在底部,因为我可能甚至没有最好的方法在这里,从一开始。谢谢_Javascript_Arrays_Object - Fatal编程技术网

JavaScript中的变异对象 有帮助的注意:考虑阅读“目的是什么”部分在底部,因为我可能甚至没有最好的方法在这里,从一开始。谢谢

JavaScript中的变异对象 有帮助的注意:考虑阅读“目的是什么”部分在底部,因为我可能甚至没有最好的方法在这里,从一开始。谢谢,javascript,arrays,object,Javascript,Arrays,Object,嘿!所以我有这个: parent: { child1: { prop: 'hi there' }, child2: { prop: 'hey there' } } 我想运行一些值映射魔术,使其如下所示: newObj: { child1: 'hi there', child2: 'hey there' } 我希望映射函数(或此过程的正确名称)从本质上获取parent中每个子项的prop值,并将该值设置为键的值部分

嘿!所以我有这个:

parent: {
    child1: {
        prop: 'hi there'
    },
    child2: {
        prop: 'hey there'
    }
}
我想运行一些值映射魔术,使其如下所示:

newObj: {
    child1: 'hi there',
    child2: 'hey there'
}
我希望映射函数(或此过程的正确名称)从本质上获取
parent
中每个子项的
prop
值,并将该值设置为
键的
部分,该键与
parent
上的同名,如果有意义的话

每个子对象上的属性是否始终命名为
prop

为什么不从一开始就这样定义呢?
因为除了
prop
之外,我还有其他属性需要使用

目的是什么?
我用的是拉威尔。当我将其发送到服务器进行验证时,我必须验证
child1.prop
,而不是
child1
,这意味着错误消息将以以下形式发回:

child1.prop:“有些东西是无效的”

。。。我希望是这样

孩子1:“有些东西是无效的”


如果有更好的方法,请提出建议。

只需添加@Chris G的答案,其他用户就可以轻松找到:

const父项={
儿童1:{
道具:“你好”
},
儿童2:{
道具:“嘿,那里”
}
};
const newObj=Object.entries(parent).reduce((a[key,{prop}])=>({…a[key]:prop}),{});
out.textContent=JSON.stringify(newObj);

您在找这个吗@为什么不回答?我只是想说清楚,我故意没有给出答案。首先,这个问题需要大量编辑。它是关于改变一个对象,与laravel或validation无关。第二,假设其他人首先会发现这个问题,他们不太可能从中受益。所以不是一个“帮助我解决我的问题”的网站;其目的是从本质上构建一个详尽的FAQ。这个问题/答案对这个问题毫无帮助。是的,我同意你的看法。我建议对此问题进行编辑,但尚未获得批准。