我有以下内容
{
bill: [
{ satisfy: 'true', comments: '' }
],
permission_title: [
{ satisfy: 'false', comments: '5' }
],
final_status: [
{ satisfy: 'true', comments: '' }
]
}
我需要发送:
{
bill: { satisfy: 'true', comments: '' },
permission_title: { satisfy: 'false', comments: '5' },
final_status: { satisfy: 'true', comments: '' }
}
获得它的最佳且快速的方法是什么?
最佳答案
假设您的对象名为obj。我会这样做:
for(var p in obj) obj[p] = obj[p][0];
const obj={
bill: [
{ satisfy: 'true', comments: '' }
],
permission_title: [
{ satisfy: 'false', comments: '5' }
],
final_status: [
{ satisfy: 'true', comments: '' }
]
}
let obj_new=Object.assign(obj);
for(var p in obj_new) { obj_new[p] = obj_new[p][0] };
console.log(obj_new);
关于Javascript 数组到对象的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223225/