什么是最好的转换方式
const mockResults = [
[{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
[{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
[{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];
到
const mockResults = [
{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' },
{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' },
{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }
];
整个任务是将mockResults
转换为requiredFormat
,这就是为什么我需要删除嵌套数组的原因:
const requiredFormat = [
{
status: 'WRONG',
data: [{ user: {firstName: '1', lastName: '1'}}, { user: {firstName: '2', lastName: '2'}}],
},
{
status: 'CORRECT',
data: [{ user: {firstName: '3', lastName: '3'}}],
},
];
这是我到目前为止尝试过的: https://jsfiddle.net/9uLje3sg/
谢谢!
答案 0 :(得分:4)
您可以使用Array javascript对象中的flat方法。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
如果要更改数据源并更改其形状,则使用map和reduce方法可以为您提供帮助。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
在您的精确示例中,reduce将适合您在按状态属性创建新的对象分组时使用。
const mockResults = [
[{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
[{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
[{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];
const flattedAndReduced = mockResults.flat().reduce( (acc, curr)=>{
const statusIndex = { 'WRONG' : 0, 'CORRECT': 1 };
acc[statusIndex[curr.status]].data.push({ user: curr.user} );
return acc;
}, [
{
status: 'WRONG',
data: [],
},
{
status: 'CORRECT',
data: [],
}
]
);
console.log(flattedAndReduced);
答案 1 :(得分:2)
按如下所示使用函数map
,该函数将返回包含所需对象的数组。
let result = mockResults.map(([user]) => user);
该方法假设原始数组中每个数组只有一个索引。
根据
的方法requiredFormat
您可以使用函数reduce
进行分组,而使用函数Object.values
进行所需的输出。
const mockResults = [
[{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
[{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
[{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];
let requiredFormat = Object.values(mockResults.reduce((a, [{user, status}]) => {
(a[status] || (a[status] = {data: [], status})).data.push(user);
return a;
}, Object.create(null)));
console.log(requiredFormat);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:1)
只需使用here即可从第一个索引返回对象。
请注意:用Array.prototype.map()
声明的变量无法修改,请改用* const
。
let mockResults = [
[{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
[{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
[{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];
mockResults = mockResults.map(i => i[0]);
console.log(mockResults);
答案 3 :(得分:0)
const mockResults = [
[{ user: { firstName: '1', lastName: '1' }, status: 'WRONG' }],
[{ user: { firstName: '2',lastName: '2' }, status: 'WRONG' }],
[{ user: { firstName: '3',lastName: '3' }, status: 'CORRECT' }]
];
const requiredFormat = [
{status: 'WRONG', data: []},
{status: 'CORRECT', data: []},
];
for(let [{user,status}] of mockResults) {
requiredFormat[ status==="WRONG" ? 0 : 1].data.push({user});
}
console.log(requiredFormat);