gpt4 book ai didi

javascript - 使用嵌套映射操作从数组返回对象

转载 作者:行者123 更新时间:2023-11-30 11:35:26 28 4
gpt4 key购买 nike

以下查询将 json 数据转换为对象 - 第一级 map(这里没问题 - 我希望结果是这样的)

const customerOptions = () => {
return customersQuery.edges.map(({ node: { id, name } }) => {
return { key: id, text: name, value: id };
});
};

输出 enter image description here

但是,如果我使用嵌套的 map 操作迭代我的 json 数据,我得到的结果是一个对象数组 - (这里我希望返回数据与第一个查询的格式相同)

const departmentOptions = () => {
return customersQuery.edges.map(({ node: { departments } }) => {
return departments.map(({ id, name }) => {
return { key: id, text: name, value: id };
});
});
};

enter image description here

我想要实现的是将二级数组映射作为对象列表返回,因此最终结果将是一个包含 40 个对象的列表。

示例 JSON

{
"data": {
"customersQuery": {
"edges": [
{
"node": {
"id": 20,
"name": "Customer name 20",
"departments": [
{
"id": 39,
"name": "Department name 20"
},
{
"id": 40,
"name": "Department name 20"
}
]
}
},
{
"node": {
"id": 19,
"name": "Customer name 19",
"departments": [
{
"id": 37,
"name": "Department name 19"
},
{
"id": 38,
"name": "Department name 19"
}
]
}
},
{
"node": {
"id": 18,
"name": "Customer name 18",
"departments": [
{
"id": 35,
"name": "Department name 18"
},
{
"id": 36,
"name": "Department name 18"
}
]
}
},
{
"node": {
"id": 17,
"name": "Customer name 17",
"departments": [
{
"id": 33,
"name": "Department name 17"
},
{
"id": 34,
"name": "Department name 17"
}
]
}
},
{
"node": {
"id": 16,
"name": "Customer name 16",
"departments": [
{
"id": 31,
"name": "Department name 16"
},
{
"id": 32,
"name": "Department name 16"
}
]
}
},
{
"node": {
"id": 15,
"name": "Customer name 15",
"departments": [
{
"id": 29,
"name": "Department name 15"
},
{
"id": 30,
"name": "Department name 15"
}
]
}
},
{
"node": {
"id": 14,
"name": "Customer name 14",
"departments": [
{
"id": 27,
"name": "Department name 14"
},
{
"id": 28,
"name": "Department name 14"
}
]
}
},
{
"node": {
"id": 13,
"name": "Customer name 13",
"departments": [
{
"id": 25,
"name": "Department name 13"
},
{
"id": 26,
"name": "Department name 13"
}
]
}
},
{
"node": {
"id": 12,
"name": "Customer name 12",
"departments": [
{
"id": 23,
"name": "Department name 12"
},
{
"id": 24,
"name": "Department name 12"
}
]
}
},
{
"node": {
"id": 11,
"name": "Customer name 11",
"departments": [
{
"id": 21,
"name": "Department name 11"
},
{
"id": 22,
"name": "Department name 11"
}
]
}
},
{
"node": {
"id": 10,
"name": "Customer name 10",
"departments": [
{
"id": 13,
"name": "Department name 10"
},
{
"id": 14,
"name": "Department name 10"
}
]
}
},
{
"node": {
"id": 9,
"name": "Customer name 9",
"departments": [
{
"id": 11,
"name": "Department name 9"
},
{
"id": 12,
"name": "Department name 9"
}
]
}
},
{
"node": {
"id": 8,
"name": "Customer name 8",
"departments": [
{
"id": 19,
"name": "Department name 8"
},
{
"id": 20,
"name": "Department name 8"
}
]
}
},
{
"node": {
"id": 7,
"name": "Customer name 7",
"departments": [
{
"id": 15,
"name": "Department name 7"
},
{
"id": 16,
"name": "Department name 7"
}
]
}
},
{
"node": {
"id": 6,
"name": "Customer name 6",
"departments": [
{
"id": 17,
"name": "Department name 6"
},
{
"id": 18,
"name": "Department name 6"
}
]
}
},
{
"node": {
"id": 5,
"name": "Customer name 5",
"departments": [
{
"id": 9,
"name": "Department name 5"
},
{
"id": 10,
"name": "Department name 5"
}
]
}
},
{
"node": {
"id": 4,
"name": "Customer name 4",
"departments": [
{
"id": 7,
"name": "Department name 4"
},
{
"id": 8,
"name": "Department name 4"
}
]
}
},
{
"node": {
"id": 3,
"name": "Customer name 3",
"departments": [
{
"id": 5,
"name": "Department name 3"
},
{
"id": 6,
"name": "Department name 3"
}
]
}
},
{
"node": {
"id": 2,
"name": "Customer name 2",
"departments": [
{
"id": 3,
"name": "Department name 2"
},
{
"id": 4,
"name": "Department name 2"
}
]
}
},
{
"node": {
"id": 1,
"name": "Customer name 1",
"departments": [
{
"id": 1,
"name": "Department name 1"
},
{
"id": 2,
"name": "Department name 1"
}
]
}
}
]
}
}
}

作为引用,这是我的 GraphQL 查询的样子 enter image description here

最佳答案

我会让外部函数调用 reduce() 而不是 map()concat()push .apply() reducer 函数中的内部 map():

使用concat():

let customersQuery = {"edges":[{"node":{"id":20,"name":"Customer name 20","departments":[{"id":39,"name":"Department name 20"},{"id":40,"name":"Department name 20"}]}},{"node":{"id":19,"name":"Customer name 19","departments":[{"id":37,"name":"Department name 19"},{"id":38,"name":"Department name 19"}]}},{"node":{"id":18,"name":"Customer name 18","departments":[{"id":35,"name":"Department name 18"},{"id":36,"name":"Department name 18"}]}},{"node":{"id":17,"name":"Customer name 17","departments":[{"id":33,"name":"Department name 17"},{"id":34,"name":"Department name 17"}]}},{"node":{"id":16,"name":"Customer name 16","departments":[{"id":31,"name":"Department name 16"},{"id":32,"name":"Department name 16"}]}},{"node":{"id":15,"name":"Customer name 15","departments":[{"id":29,"name":"Department name 15"},{"id":30,"name":"Department name 15"}]}},{"node":{"id":14,"name":"Customer name 14","departments":[{"id":27,"name":"Department name 14"},{"id":28,"name":"Department name 14"}]}},{"node":{"id":13,"name":"Customer name 13","departments":[{"id":25,"name":"Department name 13"},{"id":26,"name":"Department name 13"}]}},{"node":{"id":12,"name":"Customer name 12","departments":[{"id":23,"name":"Department name 12"},{"id":24,"name":"Department name 12"}]}},{"node":{"id":11,"name":"Customer name 11","departments":[{"id":21,"name":"Department name 11"},{"id":22,"name":"Department name 11"}]}},{"node":{"id":10,"name":"Customer name 10","departments":[{"id":13,"name":"Department name 10"},{"id":14,"name":"Department name 10"}]}},{"node":{"id":9,"name":"Customer name 9","departments":[{"id":11,"name":"Department name 9"},{"id":12,"name":"Department name 9"}]}},{"node":{"id":8,"name":"Customer name 8","departments":[{"id":19,"name":"Department name 8"},{"id":20,"name":"Department name 8"}]}},{"node":{"id":7,"name":"Customer name 7","departments":[{"id":15,"name":"Department name 7"},{"id":16,"name":"Department name 7"}]}},{"node":{"id":6,"name":"Customer name 6","departments":[{"id":17,"name":"Department name 6"},{"id":18,"name":"Department name 6"}]}},{"node":{"id":5,"name":"Customer name 5","departments":[{"id":9,"name":"Department name 5"},{"id":10,"name":"Department name 5"}]}},{"node":{"id":4,"name":"Customer name 4","departments":[{"id":7,"name":"Department name 4"},{"id":8,"name":"Department name 4"}]}},{"node":{"id":3,"name":"Customer name 3","departments":[{"id":5,"name":"Department name 3"},{"id":6,"name":"Department name 3"}]}},{"node":{"id":2,"name":"Customer name 2","departments":[{"id":3,"name":"Department name 2"},{"id":4,"name":"Department name 2"}]}},{"node":{"id":1,"name":"Customer name 1","departments":[{"id":1,"name":"Department name 1"},{"id":2,"name":"Department name 1"}]}}]};

const departmentOptions = () => {
return customersQuery.edges.reduce((result, { node: { departments } }) => {
return result.concat(departments.map(({ id, name }) => {
return { key: id, text: name, value: id };
}));
}, []);
};

console.log(departmentOptions());

使用push.apply():

let customersQuery = {"edges":[{"node":{"id":20,"name":"Customer name 20","departments":[{"id":39,"name":"Department name 20"},{"id":40,"name":"Department name 20"}]}},{"node":{"id":19,"name":"Customer name 19","departments":[{"id":37,"name":"Department name 19"},{"id":38,"name":"Department name 19"}]}},{"node":{"id":18,"name":"Customer name 18","departments":[{"id":35,"name":"Department name 18"},{"id":36,"name":"Department name 18"}]}},{"node":{"id":17,"name":"Customer name 17","departments":[{"id":33,"name":"Department name 17"},{"id":34,"name":"Department name 17"}]}},{"node":{"id":16,"name":"Customer name 16","departments":[{"id":31,"name":"Department name 16"},{"id":32,"name":"Department name 16"}]}},{"node":{"id":15,"name":"Customer name 15","departments":[{"id":29,"name":"Department name 15"},{"id":30,"name":"Department name 15"}]}},{"node":{"id":14,"name":"Customer name 14","departments":[{"id":27,"name":"Department name 14"},{"id":28,"name":"Department name 14"}]}},{"node":{"id":13,"name":"Customer name 13","departments":[{"id":25,"name":"Department name 13"},{"id":26,"name":"Department name 13"}]}},{"node":{"id":12,"name":"Customer name 12","departments":[{"id":23,"name":"Department name 12"},{"id":24,"name":"Department name 12"}]}},{"node":{"id":11,"name":"Customer name 11","departments":[{"id":21,"name":"Department name 11"},{"id":22,"name":"Department name 11"}]}},{"node":{"id":10,"name":"Customer name 10","departments":[{"id":13,"name":"Department name 10"},{"id":14,"name":"Department name 10"}]}},{"node":{"id":9,"name":"Customer name 9","departments":[{"id":11,"name":"Department name 9"},{"id":12,"name":"Department name 9"}]}},{"node":{"id":8,"name":"Customer name 8","departments":[{"id":19,"name":"Department name 8"},{"id":20,"name":"Department name 8"}]}},{"node":{"id":7,"name":"Customer name 7","departments":[{"id":15,"name":"Department name 7"},{"id":16,"name":"Department name 7"}]}},{"node":{"id":6,"name":"Customer name 6","departments":[{"id":17,"name":"Department name 6"},{"id":18,"name":"Department name 6"}]}},{"node":{"id":5,"name":"Customer name 5","departments":[{"id":9,"name":"Department name 5"},{"id":10,"name":"Department name 5"}]}},{"node":{"id":4,"name":"Customer name 4","departments":[{"id":7,"name":"Department name 4"},{"id":8,"name":"Department name 4"}]}},{"node":{"id":3,"name":"Customer name 3","departments":[{"id":5,"name":"Department name 3"},{"id":6,"name":"Department name 3"}]}},{"node":{"id":2,"name":"Customer name 2","departments":[{"id":3,"name":"Department name 2"},{"id":4,"name":"Department name 2"}]}},{"node":{"id":1,"name":"Customer name 1","departments":[{"id":1,"name":"Department name 1"},{"id":2,"name":"Department name 1"}]}}]};

const departmentOptions = () => {
return customersQuery.edges.reduce((result, { node: { departments } }) => {
result.push.apply(result, departments.map(({ id, name }) => {
return { key: id, text: name, value: id };
}));

return result;
}, []);
};

console.log(departmentOptions());

如果您的内部数组非常大,

concat() 会更好,因为它使用堆内存来将数据从 result 数组复制到一个新数组中每次。

如果您的内部数组相对较小,

push.apply() 会更好,因为它使用堆栈内存,并将数据附加到现有 result 数组的末尾, 并且不会每次都执行显式复制。

关于javascript - 使用嵌套映射操作从数组返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44654080/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com