Using JavaScript to Swap Objects in Array by ID

After retrieving a series of objects from an API and storing them in an array, I have encountered a situation like this:

array = [
 {id: 0, name: "First", relationship: "Friend"},
 {id: 1, name: "Second", relationship: "Friend"}
]

Users can freely add or remove objects to this list, which is displayed in a Vue.JS DataTable. However, there is a limit of 4 objects (let's say 4 "friends") that users can have in the array.

I am looking for suggestions on how to create a function that searches the existing array (possibly populated from the API) and places the new object in the slot corresponding to the missing ID. For example, if a user deletes the object with the ID 2 and adds another one, the function should find the empty ID 2 slot in the array and insert the new object appropriately.

So far, I have tried using array.find() with conditionals to check for the presence of the specific ID value. However, this method can sometimes lead to inserting the same object multiple times. An alternative approach that I am considering involves creating a separate map containing IDs. When a user removes an object, the map would reflect the change, and vice versa when adding a new object.

If you have any suggestions or insights on how to efficiently implement this functionality, I would greatly appreciate it. Thank you!

Answer №1

In lieu of utilizing an array, I would opt to store an object in the data. This object should be indexed by id for organization:

let objects = {
  0: { id: 0, name: 'name0', relationship: 'relationship0' },
  1: { id: 1, name: 'name1', relationship: 'relationship1' },
}

Using integer keys in modern JavaScript ensures that the insertion order is maintained, effectively making this object ordered. Since the API likely returns an array, handle it like so...

// inside the function that retrieves data from the API
  let arrayFromApi = [...];
  this.objects = array.reduce((acc, obj) => {
    acc[obj.id] = obj;  // maintains insertion order
    return acc;
  }, {});

For your UI needs, if an array is necessary (referenced as "array" in the code), you can do this:

computed: {
  array() {
    return Object.values(this.objects);
  },

To add a new object and keep it in order, pay attention to the available keys. Please note that this is a linear search, but for small numbers of objects, it will suffice:

methods: {
  // assuming maxId is a constant like 4 (or 40, but maybe not 400)
  createObject(name, relationship) {
    let object = { name, relationship };
    for (let i=0; i< maxId; i++) {
      if (!this.objects[i]) {
        object.id = i;
        this.objects[i] = object;
        break;
      }
  }

Answer №2

Give this a shot:

let items = [
      {id: 0, name: "First", relationship: "Friend"},
      {id: 4, name: "Second", relationship: "Friend"},
      {id: 2, name: "Second", relationship: "Friend"},
     ]

const addItem = (newItem) => {
  let prevId = -1

  // This sorting step is crucial if ids are not already sorted.
  // In the example array provided, ids are not sequential.

  items.sort((a, b) => a.id - b.id) 
  //

  items.forEach(obj => {
      if(obj.id === prevId + 1) prevId++
      else return;
    })
  newItem = {...newItem, id: prevId + 1 }
  items.splice(prevId+1, 0, newItem)
}

addItem({name: "x", relationship: "y"})
addItem({name: "a", relationship: "b"})
addItem({name: "c", relationship: "d"})

console.log(items)

Answer №3

To accomplish this task, you can utilize the Array.find() method in combination with Array.indexOf() and Array.splice().

Check out this Live Demo for a visual representation:

// Assume we have an array of objects retrieved from an API, and the user removed the object with ID 2.
const arr = [
  {id: 0, name: "First", relationship: "Friend" },
  {id: 1, name: "Second", relationship: "Friend" },
  {id: 3, name: "Fourth", relationship: "Friend" }
];

// Locating the neighboring objects to the removed one.
const res = arr.find((obj, index) => obj.id !== index);

// Determining the position for inserting the new object.
const index = arr.indexOf(res);

// New object that the user wishes to add
const newObj = {
    id: index,
  name: "Third",
  relationship: "Friend"
}

// Inserting the new object into the array at the specified position.
arr.splice(index, 0, newObj);

// Outputting the modified array
console.log(arr);

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

Eliminate vertical divider in table with static column using element ui

#inquiry I am currently utilizing the element-ui library for my project. If you would like to learn more, please click here. One challenge I am facing is working with fixed columns in table data. https://i.sstatic.net/u2RRT.png After scrolling horizont ...

Is it possible for an independent perl script to execute a function from a website's javascript?

Looking at this complex setup, I find myself in a situation where I must find a way to trigger the $.ajax function on a webpage using a separate Perl script. The scenario involves a webpage making $.ajax calls to a Perl file, which retrieves data and send ...

Executing the slideToggle() function in JQuery with javascript

Trying to create a script that expands and collapses when users click on either the text or image. The image is a basic arrow that switches to an arrow pointing upwards when the div is expanded. The issue I'm facing is that the text disappears when t ...

AngularJS - Viewless and Issue-Free

I'm currently working on a project that involves using AngularJS and PHP. I made some changes, but now it's not functioning properly... The issue is that there are no errors in the console, Angular works (I can retrieve its version), but my vi ...

The onclick function fails to function properly following an Ajax reload of the <div> element

I have an issue with my onclick function that only works the first time. Every time the onclick function triggers an ajax request, it successfully reloads a div which contains code to retrieve values from SQL and build an HTML table using a for loop. Alth ...

Why are the HTML links generated by JS not opening in Chrome?

<a href='http://www.xyz.hu/xyz' alt='Kosár' title='Kosár'>Megtekintés</a> Additionally: - A setInterval function refreshes the sibling's content every second, although it should not affect this specific el ...

Using a for loop in JavaScript to dynamically generate HTML content within a Django project

Do you have a unique question to ask? Version: Django version 3.0.8 This block contains my JavaScript code: fetch(`/loadbox/${message.id}`) .then(response => response.json()) .then(dialog => { let detailcontent= `<div class=" ...

Arrays can be either recreated or modified at different times

I've been trying to integrate a multiple image file selector in my React application. Here is the code for my input element: <input type="file" multiple onChange={handleImageChange} /> {renderPhotos(venueImages)} These are the functions that ...

What could be causing this minimal Angular - WebTorrent configuration to fail?

The setup appears to be quite straightforward. Check out the Webtorrent usage reference here This is how I have my setup: import WebTorrent from 'webtorrent'; @Component({ selector: 'app-root', standalone: true, template: `bla` ...

Showing information retrieved from an API and rendering it on an HTML page

My aim is to showcase a list of calculated results fetched from a local server. In the console, this data appears as an array of objects, but on the webpage, it is being displayed as separate string characters for each li element. How can I display the con ...

What is the best way to horizontally align paragraphs within different divs or spans?

Currently, I have 2 divs placed side by side that each contain about 5 paragraphs. My goal is to align these paragraphs horizontally so that paragraph 2 in the first div starts at the same line as paragraph 2 in the second div (and so on for all other para ...

Retrieve the text content from a JavaScript alert using WebDriver

Currently, I am utilizing Selenium WebDriver in C# to enhance my knowledge and create automated tests. I recently encountered a particular scenario that has left me puzzled: Imagine you have a website similar to this one: . When attempting to register wit ...

What is the process by which browsers manage AJAX requests when they are made across

I have encountered an issue that is puzzling to me, and I suspect it might be due to my misunderstanding of how the browser handles AJAX requests. Just for context, I am using Codeigniter on an Apache server and triggering AJAX requests with jQuery. The b ...

JQuery is not able to render Hindi content properly

I am attempting to showcase some Hindi words using JQuery because these are essential contents that need to be displayed on every page of the website. Please note that this is a static website built with HTML and JQuery/JavaScript. Below is my JS file: in ...

Using Vuejs for Seamless Facebook Social Login

Currently utilizing Laravel Vuejs, I'm attempting to implement social login with the vue-social-auth plugin. However, upon clicking the 'login with Facebook' button, a new Facebook window opens but unfortunately the client id and redirect ar ...

Tips for Incorporating xmlhttp.responseText in If Statements

This is the code snippet from my save_custLog_data.php file: <?php $a = $_GET['custEmail']; $b = $_GET['pswrd']; $file = '/home/students/accounts/s2090031/hit3324/www/data/customer.xml'; if(file_exists($fi ...

In mvc.net 4, Ajax is only compatible with the httpGet method and not with httpPost

There are two methods, httpGet and httpPost, inside the Login action. Currently, when an ajax call is made, it only works with the httpGet method of Login. I would like it to work with the httpPost method of Login. Thank you in advance for your answer! ...

The functionality of the page becomes disrupted when multiple timers are used after the setInterval function is executed in

My webpage fetches multiple data for a table, with one of the columns displaying date and time values from which countdown timers run. Each row has its own timer and functions correctly. However, when I added JavaScript code to refresh the table every 10 ...

Please convert the code to async/await format and modify the output structure as specified

const getWorkoutPlan = async (plan) => { let workoutPlan = {}; for (let day in plan) { workoutPlan[day] = await Promise.all( Object.keys(plan[day]).map(async (muscle) => { const query = format("select * from %I where id in (%L) ...

Clearing the content div and making it reappear using javascript

I am currently utilizing jQuery's CustomBox modal feature. While it is functioning properly, I am seeking a way to hide the div behind the modal (excluding the background image) when the modal is activated. I have successfully achieved this, but I am ...