ignore json array element during iteration with $.each

In my JSON array, the store contains data for different roles and names.

[{
"role": "Executive Director",
"name": "David Z",
...},
{
"role": "Executive Director",
"name": "David Z",
...},
{
"role": "Non Executive Chairman",
"name": "Hersh M",
...},
{
"role": "Non Executive Director",
"name": "Alex C",
...},
{
"role": "Company Secretary",
"name": "Norman G",
...}]    

Using this array, I generate multiple HTML tables dynamically.

As part of an AJAX success function, I iterate through the store to create an HTML table like below:

var table = '';
table += '<tr><td......</td>';
$.each(store, function(i, data) {
   // draw row...
   // draw more rows...
});
table += '</tr></tbody>';
$("#table_d").append(table);

However, for one of the tables, I need to skip duplicate entries such as the second occurrence of David Z.

var table = '';
table += '<tr><td......</td>';
$.each(store, function(i, data) {
    if (i > 0 && store[i].name != store[i-1].name) { 
       // draw row...
       // draw more rows...
    }
});
table += '</tr></tbody>';
$("#table_d").append(table);

The array is always ordered so I can compare store[i].name against store[i-1].name to identify duplicate names.

What is the correct way to execute the loop when store[i].name != store[i-1].name?

Answer №1

It seems to me that you are asking about a specific situation, in which case, I believe the solution lies in the following code snippet:

if(i > 0)
{
    if(store[i].name != store[i-1].name)
    {
        //execute this code block
    }
}

Answer №2

When not in your loop:

let uniqueNames = new Array();

During the iteration of your loop:

if(uniqueNames.indexOf(data[i].name) == -1){
    uniqueNames.push(data[i].name);
    //place your code here
}

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

Unravel the JSON data array and seamlessly add it to a MySQL database

Looking for some help here as I am struggling with extracting Json data and inserting it into a MySQL database using PHP. Any assistance would be greatly appreciated. {"CityInfo":[{"CityCode":"5599","Name":"DRUSKININKAI"},{"CityCode":"2003","Name":"KAUNAS ...

Getting nested documents from an array in Meteor and MongoDB: A step-by-step guide

This is a continuation of my previous inquiry on this topic here on stackoverflow.com. Firstly, I want to express my gratitude to @David Weldon. With his guidance, I successfully organized my update. However, upon retrieving the data from the database, I ...

How can MessagePorts be effectively utilized in electron Js?

What are some real-world applications of using MessagePorts in Electron JS? Why is it necessary instead of just using ipcRenderer.invoke? I haven't been able to identify any practical scenarios where MessagePorts are indispensable. It seems like it&ap ...

Struggling to integrate Ajax with Angular framework

Looking to learn Angular. Attempting to integrate Angular with AJAX calls. Whenever I try to access http://localhost:9091/jerseyservlet/jerseyrest/org/orgId/DEY using a browser or any REST client, the following response is displayed: <users> & ...

What is the best way to set the source of an image using JavaScript

On a recent project, I came across this function in PHP: foreach ($reports["included_reports"] as $index => $report_name ) { if (! in_array( $report_name, $reports["excluded_reports"])) { $optional_reports .= "<div class=\"la ...

Primefaces data table triggers the event for row selection just once

When I select a row in my p:dataTable, the onRowSelect event is only firing once. I have a datatable displaying results, and when I first click on a row, the corresponding onRowSelect method is called. However, after the initial selection, the event is not ...

Tips for incorporating extensive amounts of HTML into Internet Explorer without causing excessive strain on the CPU

I have been exploring a more ajax-based approach to loading data on a page in order to avoid postbacks. I can easily retrieve server-generated HTML through an ajax call, and adding it to the DOM is straightforward with jQuery's .append or .replaceWith ...

Is there a JavaScript equivalent to the explode function in PHP with similar functionality

I'm facing an issue while attempting to split my string in JavaScript, here is the code I am using: var str = 'hello.json'; str.slice(0,4); //output hello str.slice(6,9); //output json The problem arises when trying to slice the second str ...

Plugin that collapses dropdown menus when the mouse hovers over them, powered by jQuery

My webpage has a simple set of links, and one of them triggers a dropdown menu to appear on mouseover (refer to the image below). The dropdown submenu becomes visible when the link "How fast is it?" is hovered over, and remains fixed in place due to the di ...

The tab key seems to be malfunctioning when triggered during a jquery change event

I encountered an unusual issue with my jQuery events, and I'm not entirely sure if it's a problem related to jQuery. In hopes that some jQuery experts can shed some light on this. The code snippet below was included in my HTML page to automatica ...

JavaScript regex for the 'hh:mm tt' time format

I need to validate time in the format 'hh:mm tt'. Here is an example of what needs to be matched: 01:00 am 01:10 Pm 02:20 PM This is what I have tried so far: /^\d{2}:\d{2}:\s[a-z]$/.test('02:02 am') ...

How to capture JSON data using PHP

My dilemma involves a specific URL format, which is detailed below: localhost/handikap/insert/index.php?json={"id":123456} My goal is to extract the JSON response from this URL. I have attempted to achieve this using the following code: $data = json_dec ...

Is it possible to have numerous HTML files and directories in Phonegap/Cordova along with plugins?

As I transform my web app from HTML to Cordova for enhanced device functionality, including background audio and other features, I'm encountering some challenges. Due to the original structure of my application, which consists of multiple HTML files, ...

I am attempting to automatically refresh a mysql database through ajax without the need for a button click

My database is not updating dynamically. Is there something I am missing? It was working fine before removing some field names and automatically updating user records. But now it seems there is an issue with updating dynamically. Javascript used for form ...

employ the "include" method from Django within a JavaScript function

Currently, I am working on a search page where parameters are sent via ajax, and upon receiving the queryset, I rebuild the cards. The process is quite standard and functioning properly. Below is a simplified version of the code, with many lines removed or ...

What is the best way to store multiple values in local storage using useState in react?

Currently, I am exploring how to utilize multiple values in the useState hook and perform CRUD operations in local storage. Here is an example of my code: const [Data,setData]=[[{ name:'luis', pass:'1234', //....... }]] // ...... ...

There was an issue encountered while attempting to add concurrently to the package.json

Bundle of scripts in package.json "scripts": { "begin": "node back-end/server.js", "serve": "nodemon back-end/server.js", "client-initiate": "npm start --prefix front-end", ...

Angular 6 CSS spacing dilemmas

We recently made the switch from Angular 5 to Angular 6 and noticed that there is no spacing between the buttons/icons, etc. We are looking to bring back the spaces between the buttons and icons. I have recreated the issue below. As you can see in the Ang ...

In Ruby on Rails 3.2, each request attempts to access a URL with a timestamp format of /[timestamp]?_[timestamp

Wondering why every request in my Rails Production environment is hitting the same page with a timestamp appended to it. The headers indicate that it's expecting javascript. This extra request seems to be causing a delay in loading pages, as now each ...

Learn the steps to modify or remove table information from a database using a webpage

Hey there! I'm currently working on an Ajax jQuery function that displays table data, like in the image provided. I would like to add an option for users to edit and delete the table data, with the changes reflecting in the database. It's worth n ...