将数组与唯一键一起映射
mapping arrays together with unique keys
我正在尝试将两个数组映射在一起,然后为该新数组中的值创建唯一键。我正在使用多个不同的数组。
我的阵列看起来像这样
var arr =
[
{
"title": "title 1"
"projects": [
"project1",
"project2",
"project3",
"project4",
"project5",
"project6"
],
"project_links": [
"link1",
"link2",
"link3",
"link4",
"link5",
"link6"
]
},
{
"title": "title 2"
"projects": [
"project1",
"project2",
"project3"
],
"project_links": [
"link1",
"link2",
"link3"
]
},
{
"title": "title 3"
"projects": [
"project1",
"project2",
"project3",
"project4",
"project5"
],
"project_links": [
"link1",
"link2",
"link3",
"link4",
"link5"
]
},
{
"title": "title 4"
"projects": [
"project1",
"project2",
"project3",
"project4",
"project5"
],
"project_links": [
"link1",
"link2",
"link3",
"link4",
"link5"
]
},
{
"title": "title 5"
"projects": [
"project1"
],
"project_links": [
"link1"
]
}
]
这是我试过的
for (let i = 0; i < arr.length; i++) {
var test = arr[i].projects.map((x, y) => [x, arr[i].project_links[y]])
for (let n = 0; n < test.length; n++) {
arr[i].custom = {};
arr[i].custom["projects"] = [];
arr[i].custom["projects"].push({
name: test[n][0],
link: test[n][1]
});
}
}
我希望我的最终结果看起来像这样
var arr =
[
{
"title": "title 1"
"custom": {
"projects": [
{
"project_name": "project1",
"link": "link1"
},
{
"project_name": "project2",
"link": "link2"
},
{
"project_name": "project3",
"link": "link3"
},
{
"project_name": "project4",
"link": "link5"
},
{
"project_name": "project5",
"link": "link5"
},
{
"project_name": "project6",
"link": "link6"
}
]
}
},
{
"title": "title 2"
"custom": {
"projects": [
{
"project_name": "project1",
"link": "link1"
},
{
"project_name": "project2",
"link": "link2"
},
{
"project_name": "project3",
"link": "link3"
}
]
}
},
{
"title": "title 3"
"custom": {
"projects": [
{
"project_name": "project1",
"link": "link1"
},
{
"project_name": "project2",
"link": "link2"
},
{
"project_name": "project3",
"link": "link3"
},
{
"project_name": "project4",
"link": "link4"
},
{
"project_name": "project5",
"link": "link5"
}
]
}
},
...etc
]
我能够将数组映射在一起并为值创建唯一的键名称,但是我不知道如何将这些数组放在正确的对象中。我希望能够将它推入原始 arr 而不是创建一个全新的数组。我可以稍后删除数组中不需要的部分。提前致谢。
使用 Array#map
,遍历数组。
在每次迭代中,要获取 custom.projects
,再次使用 Array#map
,遍历当前项目数组并使用第二个 parameter(索引)获取相应的链接:
const arr = [
{
title: "title 1",
projects: [ "project1", "project2", "project3", "project4", "project5", "project6" ],
project_links: ["link1", "link2", "link3", "link4", "link5", "link6"]
},
{
title: "title 2",
projects: ["project1", "project2", "project3"],
project_links: ["link1", "link2", "link3"]
},
{
title: "title 3",
projects: ["project1", "project2", "project3", "project4", "project5"],
project_links: ["link1", "link2", "link3", "link4", "link5"]
},
{
title: "title 4",
projects: ["project1", "project2", "project3", "project4", "project5"],
project_links: ["link1", "link2", "link3", "link4", "link5"]
},
{
title: "title 5",
projects: ["project1"],
project_links: ["link1"]
}
];
const res = arr.map(({ title, projects = [], project_links = [] }) => ({
title,
custom: {
projects: projects.map((project_name, i) => ({ project_name, link: project_links[i] }))
}
}));
console.log(res);
我正在尝试将两个数组映射在一起,然后为该新数组中的值创建唯一键。我正在使用多个不同的数组。
我的阵列看起来像这样
var arr =
[
{
"title": "title 1"
"projects": [
"project1",
"project2",
"project3",
"project4",
"project5",
"project6"
],
"project_links": [
"link1",
"link2",
"link3",
"link4",
"link5",
"link6"
]
},
{
"title": "title 2"
"projects": [
"project1",
"project2",
"project3"
],
"project_links": [
"link1",
"link2",
"link3"
]
},
{
"title": "title 3"
"projects": [
"project1",
"project2",
"project3",
"project4",
"project5"
],
"project_links": [
"link1",
"link2",
"link3",
"link4",
"link5"
]
},
{
"title": "title 4"
"projects": [
"project1",
"project2",
"project3",
"project4",
"project5"
],
"project_links": [
"link1",
"link2",
"link3",
"link4",
"link5"
]
},
{
"title": "title 5"
"projects": [
"project1"
],
"project_links": [
"link1"
]
}
]
这是我试过的
for (let i = 0; i < arr.length; i++) {
var test = arr[i].projects.map((x, y) => [x, arr[i].project_links[y]])
for (let n = 0; n < test.length; n++) {
arr[i].custom = {};
arr[i].custom["projects"] = [];
arr[i].custom["projects"].push({
name: test[n][0],
link: test[n][1]
});
}
}
我希望我的最终结果看起来像这样
var arr =
[
{
"title": "title 1"
"custom": {
"projects": [
{
"project_name": "project1",
"link": "link1"
},
{
"project_name": "project2",
"link": "link2"
},
{
"project_name": "project3",
"link": "link3"
},
{
"project_name": "project4",
"link": "link5"
},
{
"project_name": "project5",
"link": "link5"
},
{
"project_name": "project6",
"link": "link6"
}
]
}
},
{
"title": "title 2"
"custom": {
"projects": [
{
"project_name": "project1",
"link": "link1"
},
{
"project_name": "project2",
"link": "link2"
},
{
"project_name": "project3",
"link": "link3"
}
]
}
},
{
"title": "title 3"
"custom": {
"projects": [
{
"project_name": "project1",
"link": "link1"
},
{
"project_name": "project2",
"link": "link2"
},
{
"project_name": "project3",
"link": "link3"
},
{
"project_name": "project4",
"link": "link4"
},
{
"project_name": "project5",
"link": "link5"
}
]
}
},
...etc
]
我能够将数组映射在一起并为值创建唯一的键名称,但是我不知道如何将这些数组放在正确的对象中。我希望能够将它推入原始 arr 而不是创建一个全新的数组。我可以稍后删除数组中不需要的部分。提前致谢。
使用 Array#map
,遍历数组。
在每次迭代中,要获取 custom.projects
,再次使用 Array#map
,遍历当前项目数组并使用第二个 parameter(索引)获取相应的链接:
const arr = [
{
title: "title 1",
projects: [ "project1", "project2", "project3", "project4", "project5", "project6" ],
project_links: ["link1", "link2", "link3", "link4", "link5", "link6"]
},
{
title: "title 2",
projects: ["project1", "project2", "project3"],
project_links: ["link1", "link2", "link3"]
},
{
title: "title 3",
projects: ["project1", "project2", "project3", "project4", "project5"],
project_links: ["link1", "link2", "link3", "link4", "link5"]
},
{
title: "title 4",
projects: ["project1", "project2", "project3", "project4", "project5"],
project_links: ["link1", "link2", "link3", "link4", "link5"]
},
{
title: "title 5",
projects: ["project1"],
project_links: ["link1"]
}
];
const res = arr.map(({ title, projects = [], project_links = [] }) => ({
title,
custom: {
projects: projects.map((project_name, i) => ({ project_name, link: project_links[i] }))
}
}));
console.log(res);