Generating a collection of dictionaries using VueJS

I am working with an HTML table that displays some values and allows user input. The goal is to only select the rows where a checkbox is checked. Each row in the table has this format: https://i.sstatic.net/eKCCE.png

Below is the code for my table:

<template>
<table id="Ref" class="table table-bordered table-striped">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Type</th>
      <th scope="col">New ID</th>
      <th scope="col">New Type</th>
      <th scope="col">New URL</th>
      <th scope="col">New Expiration Date</th>
      <th scope="col"></th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="row in rows">
      <td> <a :href="row.url" target="_blank" rel="noreferrer noopener">{{ row.id }}</a></td>
      <td> {{ row.type }} </td>
      <td> <input v-model.trim="row.newID" type="text" placeholder="ID"> </td>
      <td> <input v-model.trim="row.newType" type="text" placeholder="Type"> </td>
      <td> <input v-model.trim="row.newURL" type="text" placeholder="URL"> </td>
      <td> <input v-model.trim="row.newExpDate" type="text" placeholder="Expiration Date"> </td>
      <td style="text-align: center; vertical-align: middle;"> <input v-model="row.isSelected" type="checkbox"> </td>
    </tr>
  </tbody>
</table>
</template>

The objective is to generate a list of dictionaries as follows:

[{
    "id1": "newID1",
    "type1": "newType1",
    "url1": "newURL1",
    "exp1" "expirationDate1"
},
    {
    "id2": "newID2",
    "type2": "newType2",
    "url2": "newURL2",
    "exp2" "expirationDate2"
},
    {
    "id3": "newID3",
    "type3": "newType3",
    "url3": "newURL3",
    "exp3" "expirationDate3"
}]

I attempted to use reduce, but it only gives me a dictionary, not a list.

   putInvalidsRef() {
      const selectedRef = this.rowsInvalidsRef.filter((ref) => ref.isSelected === true);
      const refsDicts = selectedRef.reduce((acc, item) => {
        acc[item.id] = item.newID || null;
        acc[item.type] = item.newType || null;
        acc[item.url] = item.newURL || null;
        acc[item.exp] = item.newExpDate || null;
        return acc;
      }, {});
      console.log(refsDict);
      this.$http.admin.putInvalidsRef(refsDict)
        .then(getInvalidsRef);
    }

If the solution is simple, forgive my lack of experience with frontend development.

Answer №1

Try using square brackets [] instead of curly braces {} when returning a list:

function putInvalidsRef() {
  const selectedRef = this.rowsInvalidsRef.filter((ref) => ref.isSelected === true);

  const refsDicts = selectedRef.reduce((acc, item) => {
    return [
      ...acc,
      {
        [item.id]: item.newID || null, // Make sure 'item' has properties id and newID
        [item.typ]: item.newType || null,
        [item.url]: item.newURL || null,
        [item.exp]: item.newExpDate || null
      }
    ]
  }, []);
  console.log(refsDicts);
  this.$http.admin.putInvalidsRef(refsDicts)
    .then(getInvalidsRef);
}

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

"Unleashing the Power of Effortless Object Unwr

Looking for a better way to convert a raw json snapshot from Firebase into a JS class. My current method is lengthy and inefficient. Does anyone have a more optimal solution? Class: class SuggestedLocation { country_slug region_slug slug marker_ty ...

Combining Two Tables Using jQuery

I am currently attempting to combine two tables into one using jQuery in the following manner: var table = document.createElement("table"); table.id = "mergedTable"; $("#mergedTable > tbody:last") .append($("#csvInfoTable2 > tbody").html()) ...

A guide to incorporating border radius to images within a composite image with sharp.js in Node.js

In my Node.js project, I am using the sharp library to combine a collection of images into a single image. Although I have successfully created the composite image, I now need to add a border radius to each of the images in the grid. Here is the code snip ...

Is there a way to determine if a string is empty, even if it contains hard returns?

I am currently working on a function that checks if a string is empty or not, but it seems to be missing the detection of new lines. export const isStrEmpty = function(text: string): boolean { return !text || text.match(/^ *$/) !== null; }; I attempted ...

An Unexpected Token Leads to a SyntaxError in Jest's CSS-Modules

I have successfully configured my jest to allow the usage of static files by following their detailed documentation. However, despite implementing the instructions correctly, I am still encountering an error: What steps can I take to resolve this issue an ...

Unable to get ng-submit function to work properly within the Laravel PHP framework

Hello everyone, I have an inquiry regarding Laravel/AngularJS. In my project, there is a form where users can post questions. However, when I click the submit button, no requests are sent (as per inspection in Google Chrome). Interestingly, the Log in int ...

Is it possible to change the tab to "home-tab" by clicking on navbar-brand in Bootstrap 5 using JavaScript?

This is my first venture into web development, so please bear with me as I navigate through this unfamiliar territory. In my project, I have a function called Navbar() that generates a navigation bar on the webpage by wrapping all elements with the navbar ...

Wildcard GET requests in Express cause routing issues when navigating to a React application

My project is derived from the React-Webpack boilerplate repository found at this link: (https://github.com/wallacyyy/webpack-heroku/blob/master/server.js). Initially, everything was working fine but now I want to add routing within my React application. T ...

Node.js request.url is returning incomplete URL

I am currently testing out the code snippet provided in a beginner's book on Node.js. var http = require("http"); var url = require("url"); function onRequest(request, response) { console.log("request URL is: " + request.url); var pathName ...

The largest allowable call stack size within jQuery version 1.9.0

I have a question about poorly written code that I've been experimenting with. It's messy, but it's just for fun. I was attempting to create a "like system" where a user clicks on a button and the object ID associated with the button is sent ...

Typescript restricts dynamic property access within objects

I'm encountering some difficulties while attempting to access my object property in TypeScript: const sum = (type: string) => { return { status: 'Sum', value: data?[type].sum, }; }; sum('first') Here is a glimps ...

Changing tabs will redirect the url

Having an issue with ASP AjaxControlToolkit tabs. I want the URL to change based on the tab selected by the user. Check out the code snippet below: <asp:TabContainer ID="TabContainer1" runat="server" Width="100%" Height="100%"> <asp:TabPanel ...

Displaying local time alongside global time using PHP

I have a challenge - I am storing time in DATETIME format and now I need to display it in the local timezone. Does anyone know how to achieve this using PHP? ...

Modify the indentation format in CSS/JS

When the Tab key is pressed in an HTML page and the tabbed link gets highlighted, is it feasible to customize the style of that highlight? ...

The Highchart feature endOnTick does not properly align with the maximum value set for the x-axis

We're implementing line charts with Highchart's API and have a requirement to end the xAxis scale with the last value provided in the data and display its tick label. Data: [[2014-08-28, .434], [2014-10-17, .234], [2014-11-5, .134], [2015-01-12, ...

Practical steps for programmatically adding or removing md-disable-backdrop in md-sidenav

Can the md-disable-backdrop attribute be removed from HTML? The issue arises when opening the side navigation as the md-sidenav does not have the md-disable-backdrop attribute. After making a selection from the form displayed in the side navigation, I now ...

Generate a byte array in JavaScript

How can a byte array be created from a file in JavaScript (or Typescript) to send to a C# WebApi service and be consumable by the C# byte array method parameter? Could you provide an example of the JavaScript code? The context here is an Angular 4+ applic ...

When a link is right-clicked, the window.opener property becomes null

Currently, I am utilizing the window.opener property in JavaScript to update the parent window from a child window. The parent window simply consists of a table with data and a link to open the child window. When the child window is launched, a JavaScript ...

Having trouble with ng-click in my AngularJS Restful application

I was attempting to create CRUD operations in AngularJS. Unfortunately, I am facing an issue where my ng-click is not functioning properly. Below is the code for my list.html: <div class="container"> <input type="text" ng-controlle ...

Guide on integrating multiple v-for and v-if conditions within a Vue.js table

I need to create a table that includes several v-if and v-for statements. Here is my current attempt: <table> <span v-for="(option, optionk) in bundle_item.build_options" v-bind:key="optionk"> <span v-for ...