使用reduce转换一个2js对象211



我想写一个函数来传输这个对象


const input1 = [
{
id: "lkhj68C13h8uWh4RJz7",
post: "on XSS attacks",
gender: "Littérature",
postId: "cxTbP5EZjNCxw7rD60L7",
},
{
id: "Kek4ulyC13h8uWh4RJz7",
post: "The Maze",
gender: "Littérature",
postId: "cxTbP5EZjNCxw7rD60L7",
},
{
id: "arfstlyC13h8uWh4RJz7",
post: "Runner",
gender: "Littérature",
postId: "92poye7CF0aprDKcYh1Q",
},
];
const input2 = [
{
postId: "92poye7CF0aprDKcYh1Q",
postName: "Attalib",
postUrl: "attalib",
ville: "Casablanca",
},
{
postId: "cxTbP5EZjNCxw7rD60L7",
postName: "Atlas",
postUrl: "atlas",
ville: "Casablanca",
},
];

就像一样

const output = [
{
postId: "92poye7CF0aprDKcYh1Q",
postName: "Attalib",
postUrl: "attalib",
ville: "Casablanca",
posts: [
{
id: "arfstlyC13h8uWh4RJz7",
post: "Runner",
gender: "Littérature",
},
],
},
{
postId: "cxTbP5EZjNCxw7rD60L7",
postName: "Atlas",
postUrl: "atlas",
ville: "Casablanca",
posts: [
{
id: "Kek4ulyC13h8uWh4RJz7",
post: "The Maze",
gender: "Littérature",
},
{
id: "lkhj68C13h8uWh4RJz7",
post: "on XSS attacks",
gender: "Littérature",
},
],
},
];

我试过使用Array.reduce,但我不知道如何渲染。如果有人对此有任何后知后觉,我想使用postid合并两个输入,所以我可以使用map循环或reduce吗?如果有人能帮助我做到这一点,我将非常感激,如果你有任何reduce/map教程

const input1 = [
{
id: "lkhj68C13h8uWh4RJz7",
post: "on XSS attacks",
gender: "Littérature",
postId: "cxTbP5EZjNCxw7rD60L7",
},
{
id: "Kek4ulyC13h8uWh4RJz7",
post: "The Maze",
gender: "Littérature",
postId: "cxTbP5EZjNCxw7rD60L7",
},
{
id: "arfstlyC13h8uWh4RJz7",
post: "Runner",
gender: "Littérature",
postId: "92poye7CF0aprDKcYh1Q",
},
];
const input2 = [
{
postId: "92poye7CF0aprDKcYh1Q",
postName: "Attalib",
postUrl: "attalib",
ville: "Casablanca",
},
{
postId: "cxTbP5EZjNCxw7rD60L7",
postName: "Atlas",
postUrl: "atlas",
ville: "Casablanca",
}];
const final = input2.reduce((acc, current) => {
let filtered = input1.filter(obj => obj.postId === current.postId).map(({id,
post,
gender,
postId}) =>  { return {id,
post,
gender}});
current.posts = filtered; 
acc.push(current);
return acc;
}, []);
console.log(final)

您可以采用双循环方法,收集按其postId分组的所有post,并映射其他数组并添加post。

const
input1 = [{ id: "lkhj68C13h8uWh4RJz7", post: "on XSS attacks", gender: "Littérature", postId: "cxTbP5EZjNCxw7rD60L7" }, { id: "Kek4ulyC13h8uWh4RJz7", post: "The Maze", gender: "Littérature", postId: "cxTbP5EZjNCxw7rD60L7" }, { id: "arfstlyC13h8uWh4RJz7", post: "Runner", gender: "Littérature", postId: "92poye7CF0aprDKcYh1Q" }],
input2 = [{ postId: "92poye7CF0aprDKcYh1Q", postName: "Attalib", postUrl: "attalib", ville: "Casablanca" }, { postId: "cxTbP5EZjNCxw7rD60L7", postName: "Atlas", postUrl: "atlas", ville: "Casablanca" }],
posts = input1.reduce((r, o) => ((r[o.postId] ??= []).push(o), r), {}),
result = input2.map(o => ({ ...o, posts: posts[o.postId] || [] }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您的问题措辞很差,但我认为您要做的是使用postIdinput1数据分配给input2内的对象。您正在做的事情有点复杂,而且不是很简单,所以最好只在for循环或forEach循环中进行操作。

以下是一个快速而肮脏的概述:

for x in objects in input1 {
//get the postId
for y in objects in input2 {

//if an object contains the postId from x, add a new property to y with that information
}
}

如果你想了解如何使用Array.reduceArray.map,你可以查阅Mozilla参考或W3Schools的解释。

下次你问一个问题时,试着把它简化为你最基本的问题。记住,谷歌是你最好的朋友。祝你好运

为您的目标类型添加一个接口。

如果您想使用未知属性,请使用:

interface LooseObject {
[key: string]: any
}

最后:

const output: LooseObject[] = [];
input2.forEach(f => {
const post: LooseObject = f;
post.posts = input1.filter(f => f.postId === post.postId);
output.push(post);
})

在TypeScriptPlayground演示。

最新更新