Utilize the keys in a separate array to filter a JSON response based on their values

I am attempting to extract values from a JSON response by using key values stored in a separate array. However, when I try to log the results, I am only getting 'undefined'. Here is my code:

let fdName =["SerialNo","denDate","denShift","denTime","denWs_1","denWs_2","denVol_1","denVol_2","denPwdrMix"];

let dtValue = [{
id: 1,
SerialNo: 1,
denDate: "2019-09-09",
denShift: "Day",
denTime: "10:32:00",
denWs_1: 10.23,
denWs_2: 11.2,
denVol_1: 12.5,
denVol_2: 10.12,
denPwdrMix: 0.75,
created_at: null,
updated_at: null
}];

var savedData = fdName.map(function(e) {
    var filteredRes = dtValue.find(a => a[key] == e);
return filteredRes ;});

console.log(savedData);

Expected output:

[1, 2019-09-09, Day, 10:32:00, 10.23, 11.2, 12.5, 10.12, 0.75];

I would like to display these obtained results in a table using v-for as shown below:

<tbody>
 <tr>
  <td
    v-for="(dtValue, sdtValue ) in savedData "
     :key="sdtValue"
      >     
       {{dtValue}}
 </td>
  </tr>
 </tbody>

Answer №1

If you wish to map based on fieldName, you will require 2 loops for rendering the outcome.

let fieldName =["SerialNo","denDate","denShift","denTime","denWs_1","denWs_2","denVol_1","denVol_2","denPwdrMix"];

let dataValues = [{
  id: 1,
  SerialNo: 1,
  denDate: "2019-09-09",
  denShift: "Day",
  denTime: "10:32:00",
  denWs_1: 10.23,
  denWs_2: 11.2,
  denVol_1: 12.5,
  denVol_2: 10.12,
  denPwdrMix: 0.75,
  created_at: null,
  updated_at: null
}];

var storedData = dataValues.map((e) => fieldName.map(key => e[key]));

console.log(storedData);

This will result in the table:

<tbody>
  <tr v-for="data in storedData">
    <td v-for="value in data">     
      {{value}}
    </td>
  </tr>
</tbody>

You can also render without mapping, which requires only one loop but listing all fields individually. For example:

<tbody>
  <tr v-for="data in dataValues">
    <td>{{data[key1]}}</td>
    <td>{{data[key2]}}</td>
    <td>{{data[key3]}}</td>
    <td>{{data[key4]}}</td>
    <td>{{data[key5]}}</td>
  </tr>
</tbody>

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

perform an action if any division element is void of content

Currently, I have a statement that checks if any of the Divs with the ID #Drop are empty. If one is found to be empty, an alert is shown. However, there seems to be an issue where the statement stops working when any div contains content. What I am trying ...

Posts that are repeated when making an AJAX call in WordPress

I have been experimenting with implementing a 'load more' button feature in my WordPress site. The concept is simple - users click the button, and additional posts are loaded using AJAX without the need for page reload or pagination. Following a ...

Converting JSON to Custom Object in Android using Jackson during Spring_MAPPING

My current challenge involves passing a JSONArray from Android to my Spring Java server. I've experimented with two methods so far. One approach is to pass the JSONArray as a String and capture it using the @PathVariable annotation. With this method ...

ReactForms Deprication for NgModel

According to Angular, certain directives and features are considered deprecated and could potentially be removed in upcoming versions. In a hypothetical scenario, let's say I am using NgModel with reactive forms, which Angular has marked as deprecate ...

HTTP request in Angular with specific body content and custom headers

My goal is to access the sample API request from , as demonstrated in the documentation: curl -H "api-key: 123ABC" \ -H "Content-Type: application/json" \ -X POST \ ...

Tips for efficiently utilizing a JSON file in Robot Framework: How to read the JSON data once and reuse it multiple times within the same robot file

I have been working on implementing a test automation framework using the robot framework, but I have run into some difficulties with JSON manipulation. My robot file is filled with keywords (no tests), and I attempted to read a JSON file (which contains x ...

Sorting and searching through multiple values using data structures in Rails

Currently, I am developing a software tool that takes input data from your golf game to determine your handicap. In addition to this feature, I have also included a table on the side of the program similar to Excel. This table consists of golf course names ...

How can I combine addClass with fadeIn/fadeOut in JavaScript?

Can the addClass and fadeIn functions be combined in JS? Although I'm not very experienced with JS, I'm working on a script where the div container changes background color by fading in and out on text hover. I've created a CodePen to demo ...

Displaying Laravel's validation errors within the Ajax success event

When attempting an Ajax request and anticipating a failure, I find that the error response is unexpectedly being received within the success event instead of the fail event. I require Laravel's response to be directed to the Ajax fail event, as I int ...

Having problems uploading and resizing images using Express.js

var express = require("express"), app = express(), formidable = require('formidable'), util = require('util'), fs = require('fs-extra'), qt = require('quickthumb'); // Utilizing quickthumb ap ...

In what scenarios would you choose to use "class" instead of "id"?

I am currently utilizing Bootstrap to style my table, which serves as a basic to-do list. My intention is to insert a form input into one cell and place a check button in the cell adjacent to it on the right side. To achieve this, I plan to utilize id="che ...

Tips for implementing a Dialog Box within a Card Action Button Group

I'm currently exploring the option of incorporating a Dialog box as a button within a Button Group, which is then nested inside a CardActions component in Material UI. Previously, I had success with using regular Buttons instead of a Dialog, where th ...

Using the JQuery post method along with the name attribute

I have a basic form that I would like to submit using jQuery. In my Django code, I have some custom logic that relies on the name attribute of the form (such as if request.POST.get('BoekForm2')). Below is the structure of my page: <script sr ...

What are some ways to display unprocessed image data on a website using JavaScript?

I have an API endpoint that provides image files in raw data format. How can I display this image data on a website using the img tag or CSS background-image property, without utilizing canvas? One possible approach is shown below: $.get({ url: '/ ...

The animation in threejs using lerp and camera transitions lacks fluidity

Why does linear interpolation pose a problem? When calling zoomCamera() inside an update() function that is being animated, there is a smooth lerp effect. However, when called within the onObjectsClick() function, the lerp is sharp. function onObjectsCl ...

Difficulty in dynamically updating custom attributes data in a popover

I am attempting to dynamically insert text into a Bootstrap 3 Popover data-content="" on this demo. My goal is to update the text in the data-content="" attribute by clicking different buttons. I have tried doing this using the following code: $("#poper") ...

Update Jquery Dialog's Button After Making an Ajax Request

I am facing an issue with my dialog window that contains some confirmation elements following a form submission: $('#newUserDialog').dialog({ autoOpen: false, width: 600, modal: true, resizable: false, cl ...

The nested ng-repeat on angular seems to be malfunctioning

Utilizing an ng-repeat to display the various sections of a form, I am facing an issue with displaying the questions within each section when using a nested ng-repeat. The values are not being displayed and I'm unsure why. Below is a sample JSON stru ...

The retrieval of JSON data in a JavaScript function is malfunctioning

I am new to working with Ajax and have reached the point where I am able to retrieve data. However, I am struggling to return the data as I keep getting undefined values. Below is the code snippet: function select_aragement(arragament){ var arrst = ar ...

The Bootstrap carousel feature allows you to track the duration each image is displayed

Is there a way to track the amount of time a specific image is visible to the user in a Bootstrap 5 carousel? I'm interested in measuring how long a user views a particular image, such as a product image, in the carousel. For example, I'm lookin ...