Javascript: Generate a fresh array of interconnected objects (B) by pairing objects from an existing array of objects (A)

Since yesterday, I have successfully tackled my algorithm problem.

Recognizing that the title may be a bit vague, I will do my best to clarify my thoughts.

What I aim to do is transform a table of objects (A) into a new table of objects (B).

The array of objects (A) is structured like this:

{
  id : "REC-001",
  text: "Facebook",
  link: ["REC-002", "REC-003"]
},
{
  id : "REC-002",
  text: "Instagram",
  link: ["REC-003"]
},
{
  id : "REC-003",
  text: "Snapshat",
  link: ["REC-001", "REC-002"]
}

The desired structure for object array (B) is as follows:

{
  id : "REC-001",
  text: "Facebook",
  link: [
    {
      key: "REC-002",
      text: "Instagram"
    },
    {
      key: "REC-003",
      text: "Snapshat"
    }
  ]
},
{
  id : "REC-002",
  text: "Instagram",
  link: [
    {
      key: "REC-003",
      text: "Snapshat"
    }
  ]
},
{
  id : "REC-003",
  text: "Snapshat",
  link: [
    {
      key: "REC-001",
      text: "Facebook"
    },
    {
      key: "REC-002",
      text: "Instagram"
    }
  ]
}

Currently, the only code snippet that partially aligns with my goal looks like this (everything else has been discarded):

 for (let i = 0; i < objectA.length; i++) {
   for (let j = 0; j < objectA[i].link.length; j++) {
     console.log(i, j, objectA[i].link[j])
   };
 };

The output of the console.log is:

0 0 'REC-002'
0 1 'REC-003'
1 0 'REC-003'
2 0 'REC-001'
2 1 'REC-002'

Unfortunately, I am struggling to generate an array of "link" objects associated with the main "id". Any assistance would be greatly appreciated!

Thank you,

Sam.

Answer №1

let data = [
  {
    id : "REC-001",
    text: "Facebook",
    link: ["REC-002", "REC-003"]
  },
  {
    id : "REC-002",
    text: "Instagram",
    link: ["REC-003"]
  },
  {
    id : "REC-003",
    text: "Snapshat",
    link: ["REC-001", "REC-002"]
  }
];

let newData = data.map(item => ({
  id: item.id,
  text: item.text,
  link: item.link.map(l => {
    let foundItem = data.find(a => a.id === l);
    return {
      key: foundItem.id,
      text: foundItem.text
    };
  })
}));

console.log(newData);

Answer №2

To start, extract the id and then construct the desired nested arrays.

var data = [{ id : "REC-001", text: "Facebook", link: ["REC-002", "REC-003"] }, { id : "REC-002", text: "Instagram", link: ["REC-003"] }, { id : "REC-003", text: "Snapshat", link: ["REC-001", "REC-002"] }], ids = data.reduce((m, { id: key, text }) => m.set(key, { key, text }), new Map), result = data.map(o => ({ ...o, link: o.link.map(id => ids.get(id)) }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

An alternate method involves a single loop with a closure over a hash table to gather values later.

var data = [{ id : "REC-001", text: "Facebook", link: ["REC-002", "REC-003"] }, { id : "REC-002", text: "Instagram", link: ["REC-003"] }, { id : "REC-003", text: "Snapshat", link: ["REC-001", "REC-002"] }], result = data.map(
(m => o => (
Object.assign(m[o.id] = m[o.id] || {}, { key: o.id, text: o.text }),
{ ...o, link: o.link.map(id => m[id] = m[id] || {}) })
)
({})
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Answer №3

Here is a solution to help resolve the issue:

let originalArray = [{
  id : "REC-001",
  text: "Facebook",
  link: ["REC-002", "REC-003"]
},
{
  id : "REC-002",
  text: "Instagram",
  link: ["REC-003"]
},
{
  id : "REC-003",
  text: "Snapshat",
  link: ["REC-001", "REC-002"]
}];

let modifiedArray = originalArray.map( item => {
return {
  ...item,
    link: [{key: item.link[0], text: 'Instagram'}, {key: item.link[1], text: 'Facebook'}]
  }
});

console.log(modifiedArray);

Answer №4

This implementation should do the job, please let me know if you encounter any issues and feel free to adjust the variable names as required. (Please note that this solution is greedy and lacks optimization)

 var items = [{
      id : "REC-001",
      text: "Facebook",
      link: ["REC-002", "REC-003"]
    },
    {
      id : "REC-002",
      text: "Instagram",
      link: ["REC-003"]
    },
    {
      id : "REC-003",
      text: "Snapshat",
      link: ["REC-001", "REC-002"]
    }];
    
    items.forEach(function(item){
      let newLinks = [];
      item.link.forEach(function(linkId){
        items.forEach(function(obj){
          if(linkId === obj.id){
            newLinks.push({key : obj.id, text : obj.text });
          }
        });
         item.link = newLinks;
      });
    });
    
    console.log(items);

Answer №5

Here is some code that may be useful to you:

var ArrayX = [{
  id : "ITEM-001",
  description: "Apple",
  connection: ["ITEM-002", "ITEM-003"]
},
{
  id : "ITEM-002",
  description: "Banana",
  connection: ["ITEM-003"]
},
{
  id : "ITEM-003",
  description: "Cherry",
  connection: ["ITEM-001", "ITEM-002"]
}]

function getModifiedArray(array) {
  const formatConnection = (key) => ({
    key,
    description: array.find(el => el.id === key).description
  });

  return array.map((item) => (
    item.connection ? { ...item, connection: item.connection.map(formatConnection) } : item
  ));
}

console.log(getModifiedArray(ArrayX));

Answer №6

If you want to change the first array into a specific format, you can utilize the array methods map and find.

The Array.map method allows you to iterate over each element in the original array and modify it as needed.

On the other hand, the Array.find method is used to search for a specific element within an array based on certain criteria.

const arrayA = [
{
id: 'REC-001',
text: 'Facebook',
link: ['REC-002', 'REC-003'],
},
{
id: 'REC-002',
text: 'Instagram',
link: ['REC-003'],
},
{
id: 'REC-003',
text: 'Snapshat',
link: ['REC-001', 'REC-002'],
},
]

const arrayB = arrayA.map((mapArrayAChild) => {
return {
...mapArrayAChild,
link: mapArrayAChild.link.map((mapLinkChild) => {
const linkObject = arrayA.find((findLinkChild) => {
return findLinkChild.id === mapLinkChild
})
return {
key: linkObject.id,
text: linkObject.text,
}
}),
}
})

console.log(JSON.stringify(arrayB, null, '\t'))

To explain the process:

  1. We iterate through the elements of arrayA and create a new array with transformed elements.
const arraB = arrayA.map(functionTransformingEachChild)
  1. For each iteration, we copy the properties of the current element and paste them into a new object using the spread operator.
return {
    ...childInCurrentIteration
}
  1. We update the link property with a new array created using the map method.
return {
    ...childInCurrentIteration,
    link: childInCurrentIteration.link.map(otherFunctionTransformingEachChild)
}
  1. While transforming each element of the link array, we locate the corresponding object in the original arrayA using the key string from the current iteration (e.g., "REC-002").
mapArrayAChild.link.map(
    (mapLinkChild) => {
        const linkObject = arrayA.find(
            (linkKeyStringInCurrentIteration) => {
                return linkKeyStringInCurrentIteration.key === mapLinkChild
            }
        )
        // ...
    }
)
  1. We replace the string in the link array with a new object containing the necessary properties.
return {
    key: linkObject.id,
    text: linkObject.text
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing the power of jQuery's $.each method to dynamically generate HTML select options within an AJAX

I am working with a bootstrap modal that includes a form which requires data from the database. To retrieve this data, I am using PHP as shown below: public function get_view_for_inspection_report_belum_eor(){ $q = $this->inspection->get_view_fo ...

Trigger the function upon choosing the identical option in the select HTML tag

Within my cordova app, I have set up functions that are triggered whenever a user clicks an <option> within a <select> dropdown. This functionality is achieved using the onChange="" HTML attribute. However, I am now looking for a solution that ...

Ways to retrieve a file from a specific location using fetch/axios?

For my research, I need to utilize certain network APIs such as fetch or axios to access a local file without using the fs module or importing them directly. I attempted to use both fetch and axios but found that they do not support fetching local files, ...

How should we structure our JavaScript code: MVC or self-rendering components?

I'm in the process of developing a highly JS-centric web application. The bulk of the work is being carried out on the client side, with occasional syncing to the server using AJAX and XMPP. This is my first venture into creating something of this ma ...

The POST variable consistently remains void

The approach I am using to handle the data sent with jquery.ajax involves sending an empty string by default. Whenever there is a change, I monitor the input for changes and resend the data. Everything seems to work fine in the console, but in PHP, $this-& ...

Are JS promises compatible with any function in Angular?

I have a question: Can you use then() on any function? In my Angular app, I'm encountering an error ('cannot read property then of undefined') when attempting to utilize then. For instance, take this function: self.getCommentsData = funct ...

Uploading a file with AngularJS and storing it in a database

I have been attempting to implement ngFileUpload in order to upload images and store them in a database – specifically, a mongoLab database that accepts JSON objects which can be posted using this syntax: $http.post('myMongoName/myDb/myCollection/ ...

Sending an array of dictionary objects to a JavaScript function

I have a situation where I need to pass a large amount of data stored in an NSArray containing NSDictionary objects to a JavaScript function using a webview and the method: - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script My inquir ...

What is the process for setting up a Schema and express router in the MERN stack?

Working with a MongoDB record that looks like this: { "_id": { "$oid": "5c09ae281646e8d8bad07d73" }, "name": "somename", "genre": "somegenre", "indexes": { "index1": 1, "index2": 7, "index3": 7, ...

The combination of autocomplete and keydown functionality is a powerful tool

Implementing an autocomplete feature using jQuery/Ajax, I am seeking to bind events that trigger when the input is filled with displayed values or when the submit button is clicked: $('input[type='text']').on('keydown',functi ...

AngularJS implemented to trigger a popup alert after a certain duration of time has elapsed since the

Can we create a popup alert that says "Error Contacting Server" when the http request does not receive any response? .controller('items_ctrl',['$scope','$http',function($scope,$http){ $scope.shop_id=localStorage.getItem(" ...

The dropdown arrow icon fails to display downward direction when double-clicked

I'm working on a select dropdown that resembles a material UI dropdown. I've added icons for an arrow up and arrow down to indicate the state of the dropdown. The aim is to show the arrow up when the dropdown is open and switch to the arrow down ...

Building a Timekeeping Tool with Javascript

I am interested in creating a timer/stopwatch using JavaScript for a specific scenario: When the user clicks the "Play" button, the stopwatch will start counting, and when they click "Pause," it will stop. The difference between the start and end times wi ...

The time module in Python consistently displays a time difference of 0 when measuring the time between two lines of code

My current project involves measuring the performance of Python's contains() function. I am achieving this by utilizing the time module within Python. Below is a snippet of the code I am working with: from time import time def contains(collection, ...

Update the CSS for InputLabel

I have a drop-down list that I want to customize. The issue is illustrated below: https://i.sstatic.net/hzVtl.png I'm looking to center the text "choose format" within the field and adjust the font size. return ( <FormControl sx={{ m: 1 ...

Node.js: Implementing user redirection after successfully signing up/login on a nodejs platform

I am having an issue with redirecting a user to their profile page in node.js after they login or sign up. The login/signup system is functioning correctly and the data is being saved, but the user is not getting redirected to the correct profile page ro ...

Clicking the button will close the active tab in Firefox

I need help with a webpage that has a popup. When the user clicks the OK button in the popup, I want the current tab to close. The OK button is an HTML input tag. I've tried various ways of using JavaScript's close method and searched online, but ...

Displaying all divs when the checkboxes are unchecked

My code displays a product list with various details stored in different divs based on data attributes like brand, store, and more. A friend helped me develop a filter using checkboxes for record selection. However, I need to make a small modification to i ...

What is the method to have text added automatically to an input field when a key is pressed?

Within my navigation, I have implemented a "full-screen overlay search box". When users click on the magnifying glass icon, a full-screen overlay with a search input field appears. You can see an example image here: . Currently, to enter text in this full ...

An unexpected error occurred while attempting to establish a connection to a MySQL server using Sequelize: 'LRU is not a constructor'

I have been working on a web application that utilizes boardgame.io's Tic-Tac-Toe tutorial game, with a twist - the results of each match are saved to a MySQL database. The current state of my code involves trying to establish a connection to the data ...