Home > database >  mapping arrays together with unique keys
mapping arrays together with unique keys

Time:01-01

I am trying to map two arrays together and then create unique keys for the values in that new array. I am working with multiple different arrays.

My arrays look like this

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"
      ]
    }
  ]

This is what I have tried


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]
        });
    }
}

I want my end result to look like this

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
]

I'm able to map the arrays together and create unique key names for the values, however I don't know how I can have those arrays in the correct objects. I want to be able to push it into the original arr instead of creating a brand new array. I can delete the unwanted parts of the array later. Thanks in advance.

CodePudding user response:

Using Array#map, iterate over the array.

In every iteration, to get custom.projects, again using Array#map, iterate over the current projects array and using the second parameter (index) get the corresponding links:

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);

  • Related