Comparing Two Tables through their Values

To effectively compare these tables, first by value and identify any differences. If a match does not exist, indicate that as well. Take a look at the attached image for reference. The second table contains two individuals with identical information but different ages, whereas the first table has one individual matching the details in the second table. It is crucial to highlight the non-existent entry in the first table. The end result should mirror the visual representation shown in the image: https://i.sstatic.net/qVTrM.jpg

    const firstArr = [
        { partNo: '1',name: 'Per', age: '35'},
        { partNo: '3',name: 'Tom', age: '27'},
        {partNo: '6', name: 'Alex',age: '18'}
    ]
    const secondArr = [
        { partNo: '1', name: 'Per', age: '33'},
        { partNo: '3', name: 'Tom', age: '27'},
        { partNo: '6', name: 'Alex', age: '18'},
        { partNo: '6',  name: 'Alex', age: '22'}
    ]


let table1 = `<table><tr><th>PartNo</th><th>Name</th><th>Age</th></tr>`
let table2 = `<table><tr><th>PartNo</th><th>Name</th><th>Age</th></tr>`

for (let i = 0; i < firstArr.length; i++) {   
for (let j = 0; j < secondArr.length; j++) {
    table1 += `<tr><td>${firstArr[i].partNo}</td><td>${firstArr[i].name}</td><td>${firstArr[i].age}</td></tr>`
    table2 +=  `<tr><td>${secondArr[j].partNo}</td><td>${secondArr[j].name}</td><td>${secondArr[j].age}</td></tr>`
}
}
table1 += `</table>`
table2 += `</table>`
document.getElementById('firstTable').innerHTML = table1
document.getElementById('secondTable').innerHTML = table2
<div style="display: flex;">
<div id="firstTable"></div>
<div>''''''''</div>
<div id="secondTable"></div>
</div>

Answer №1

There seems to be an issue with your loop structure, where the second array's loop is nested within the first one


If you use forEach, it can make your code cleaner and easier to manage

const firstArr = [{
    partNo: '1',
    name: 'Per',
    age: '35'
  },
  {
    partNo: '3',
    name: 'Tom',
    age: '27'
  },
  {
    partNo: '6',
    name: 'Alex',
    age: '18'
  }
]
const secondArr = [{
    partNo: '1',
    name: 'Per',
    age: '33'
  },
  {
    partNo: '3',
    name: 'Tom',
    age: '27'
  },
  {
    partNo: '6',
    name: 'Alex',
    age: '18'
  },
  {
    partNo: '6',
    name: 'Alex',
    age: '22'
  }
]


let table1 = `<table><tr><th>PartNo</th><th>Name</th><th>Age</th></tr>`
let table2 = `<table><tr><th>PartNo</th><th>Name</th><th>Age</th></tr>`

firstArr.forEach(element => table1 += `<tr><td>${element.partNo}</td><td>${element.name}</td><td>${element.age}</td></tr>`);

secondArr.forEach(element => table2 += `<tr><td>${element.partNo}</td><td>${element.name}</td><td>${element.age}</td></tr>`);

table1 += `</table>`
table2 += `</table>`
document.getElementById('firstTable').innerHTML = table1
document.getElementById('secondTable').innerHTML = table2
<div style="display: flex;">
  <div id="firstTable"></div>
  <div>''''''''</div>
  <div id="secondTable"></div>
</div>


If you prefer using a for loop, ensure that you separate the loops for each array

const firstArr = [{
    partNo: '1',
    name: 'Per',
    age: '35'
  },
  {
    partNo: '3',
    name: 'Tom', 
    age: '27' 
  },
  {
    partNo: '6',
    name: 'Alex', 
    age: '18' 
  } 
] 
const secondArr = [{ 
    partNo: '1', 
    name: 'Per', 
    age: '33' 
  }, 
  { 
    partNo: '3', 
    name: 'Tom', 
    age: '27' 
  }, 
  { 
    partNo: '6', 
    name: 'Alex', 
    age: '18' 
  }, 
  { 
    partNo: '6', 
    name: 'Alex', 
    age: '22' 
  } 
]

let table1 = `<table><tr><th>PartNo</th><th>Name</th><th>Age</th></tr>`
let table2 = `<table><tr><th>PartNo</th><th>Name</th><th>Age</th></tr>`

for (let i = 0; i < firstArr.length; i++) {
  table1 += `<tr><td>${firstArr[i].partNo}</td><td>${firstArr[i].name}</td><td>${firstArr[i].age}</td></tr>`
}
for (let j = 0; j < secondArr.length; j++) {
  table2 += `<tr><td>${secondArr[j].partNo}</td><td>${secondArr[j].name}</td><td>${secondArr[j].age}</td></tr>`
}

table1 += `</table>`
table2 += `</table>`
document.getElementById('firstTable').innerHTML = table1
document.getElementById('secondTable').innerHTML = table2
<div style="display: flex;">
  <div id="firstTable"></div>
  <div>''''''''</div>
  <div id="secondTable"></div>
</div>

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

Is JSP being double-dipped via an Ajax request?

I'm encountering an issue with a JSP1 that is using Ajax to pass a variable to another JSP2. It seems like JSP2 is being loaded twice, and I can't figure out why. When I check my app console log, I see the following: my_id = 77An2J my_id = null ...

JQuery is displaying the word "undefined" instead of the expected JSON array values

Currently, I'm in the process of developing a PhoneGap application that utilizes JQuery and AJAX to interact with JSON data from a PHP file. Specifically, there's a part of my app where I want users to click a button and have it display a list of ...

Exploring the usage of multidimensional arrays in React components

How can I extract data such as teamId from the "teams" array using datas.map() method in a multidimensional array? Any tips or help would be appreciated. Is there another way to map out this data? { "gameId": 3226262256, "platformId": "NA1", " ...

Guide on triggering a bootstrap popup modal using a TypeScript file

I am currently working on an Angular project where I need to launch a popup modal when my function is called. I came across an example on w3schools, but it only contains the HTML logic to open the popup. What I want to achieve is to open the popup from th ...

Resolving the $rootScope:infdig infinite looping issue

I understand the concept of the infinite digest loop and how it occurs, but I am encountering an issue. You can see my code and problem demonstrated in this fiddle: http://jsfiddle.net/eS5e5/1/ If you check the console in the jsfiddle, you'll notice ...

How can I stack one column beneath two columns in a unique format?

I am looking to create a layout similar to the one depicted below. https://i.sstatic.net/m1aCj.png The window is represented by the red box, and the black boxes represent the <div> elements. I have attempted to arrange the image into two columns, bu ...

Using a combination of jQuery and JavaScript to switch image tags between different classes

I'm really struggling with this issue and could use some guidance. Essentially, I have a code that should change the class of an img tag when a user clicks on a div element. Let's take a look at the HTML structure: <li><img src ...

Serialization of JSON is not possible for the data type <code>[object Promise]</code>

Full error: Error: Issue when serializing data .b retrieved from getStaticProps in "/". Cause: object ("[object Promise]") cannot be serialized as JSON. Please ensure only JSON serializable data types are returned. Encountering an er ...

Guide on exporting type definitions and utilizing them with npm link for a local package

I am in the process of developing a new testing tool called tepper as an alternative to supertest. My goal is to make this package available in both ESM and CJS formats. However, I'm encountering an issue where users of the library are unable to locat ...

Adding the header.php file in WordPress caused the style.css to stop working

Whenever I include a basic "header.php" file in my theme directory, my style.css mysteriously stops working. But when I remove the "header.php" file, everything goes back to normal. How can I troubleshoot this issue? Below is the code snippet from my fil ...

add to array object if it does not already exist

I am currently working with the following model in JavaScript and utilizing AngularJS: $scope.data = { FocusOn: " ", Filters: [], Range: { From: "", To: "" } } Additi ...

Select the final word and enclose it within a span class

I have a task where I need to identify the last word in a class and then enclose it with a span element so that I can style it using JavaScript. <h1 class="title>A long tile</h1> <h2 class="title>A long tile</h2> should b ...

Remove text from input field and deactivate button upon clicking in AngularJS

I am facing an issue where I want to disable the submit button until some text is entered in the input box. <div id="app-id-input-container" ng-form="appIdInput"> <div class="input-group"> <input id="app-id-input" name="appIdInp ...

Integrate jQuery into your Django project

Having trouble integrating jQuery into my Django app. Not sure what went wrong as the button click is not producing any results. Here's a snippet of my code: base.html {% load static %} <html> <head> <link rel ...

Utilizing the VueJS Multiselect Plugin, there is a requirement to reset the dropdown selection upon successful form submission

I recently started using the Vue Js Multiselect plugin and you can find more about it here. I am currently utilizing it for single search purposes. The issue I am facing is that I need to reset the selected values in the dropdown once my form has been succ ...

Effortless Page Scrolling with Dynamic Title Changes

Currently, on my website, the page title changes to the name of the song that is currently playing. However, I want the title to continuously scroll so that users can see the full song name even with multiple tabs open. I experimented with various scrolli ...

JavaScript: When setting focus on a DOM element, the document.activeElement is not automatically updated

I am facing an issue where I need to manually set focus on two buttons consecutively. These buttons are jQuery-Objects stored in an array named pMenus. Below is the code snippet: function OpenSubMenus(pMenus) { pMenus[pMenus.length - 1].get(0).focus() ...

The form will only display results after the "Submit" button is clicked twice

Recently, I built a flask website where the form and results are displayed on the same page. However, there seems to be an issue that arises upon clicking the 'submit' button for the first time after running 'flask run'. The error messa ...

Unable to populate dropdown list using jquery, ajax, and json

I am so close to figuring it out, but I am struggling with iterating through JSON objects to populate a dropdown list. Here is the JavaScript code I have written: The JSON data I am working with looks like this: <pre>{"name":"County1","name":"County ...

The Datatable plugin is unable to effectively customize tables that are loaded dynamically

Within my single page application, the home page initially loads all static resources and js files. Subsequently, upon a user click event, the table is dynamically loaded. Unfortunately, the datatable customization does not seem to be applied on the newly ...