Display the two values of an object pair using ng-repeat in AngularJS

I have an array of objects structured like this:

myCtrl.siteNameLabels = myCtrl.actual.map(function (value, index) {
    return {
        actualSite: {
            actualSiteName : value,
            actualSiteData:  myCtrl.tableData[index]
        },
        pastSite: {
            pastSiteName : myCtrl.pastSiteNameLabels[index] + "_past",
            pastSiteData : myCtrl.tableDataPast[index]
        }
    }
})

The array contains pairs of sites (current and past), each with a shared name where the past site is identified by having "_past" at the end. Both current and past sites have associated data in the form of numeric arrays.

While every site has present data, not all sites have past data available.

The challenge lies in displaying this data in a table header as follows:

Site1 | Site1_past | Site2 | Site3 | Site3_past | Site4

If only present site data was present, it could easily be displayed using an ng-repeat. However, the task includes integrating the optional past site data alongside their corresponding present sites.

This code snippet demonstrates how to generate the display for present sites:

<thead>
    <tr>    
        <th ng-repeat="label in $ctrl.siteNameLabels">{{label.actual.actualSiteName}}</th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="rows in $ctrl.tableData[0] track by $index">
        <td>{{$ctrl.dateLabels[$index]}}</td>
        <td ng-repeat="label in $ctrl.actualSiteNameLabels">{{$ctrl.tableData[$index][$parent.$index]}}</td>
    </tr>
</tbody>

This would render the display as:

Site1 |  Site2 | Site3 | Site4

Do you have any suggestions on how to modify it to achieve the desired format?

Site1 | Site1_past | Site2 | Site3 | Site3_past | Site4

Answer №1

Here is a suggestion for saving site information:

    site: {
        currentSiteName : value,
        currentSiteData: myCtrl.tableData[index],
        previousSiteName: myCtrl.previousSiteNames[index] + "_previous",
        previousSiteData: myCtrl.tableDataPrevious[index]
    }

Fingers crossed that this does not impact any other features...

Answer №2

let siteList = [{ name: "Site1", pastName: "Site1_past" },
                 { name: "Site2" },
                 { name: "Site3", pastName: "Site3_past" },
                 { name: "Site4" },
                ];

let sitesFlatList = [];
siteList.forEach(function(site) {
    sitesFlatList.push(site.name);
    if (site.pastName) {
        sitesFlatList.push(site.pastName);
    };
});
console.log(sitesFlatList);   

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

Utilizing Array in ReactJS with the Power of Hooks

My current situation involves having an array of FooBar objects interface FooBar { foo: string, bar: string, correct: string, other: string[] } const [arrOfObj, setArrOfObj] = useState<FooBar[]>([ { "foo": "fool ...

Retrieve highlighted text along with its corresponding tag in ReactJS

my <span class="highlight">highlighted</span> word The text above is showing an example including HTML tags. However, when using window.getSelection(), only the text "my highlighted word" is returned without the surrounding <span& ...

Splitting the package.json file to separate the front-end and back-end components while utilizing shared

Currently, I am working on a project that involves a separate frontend (webpack) and backend (express/mongodb). My goal is to separate the dependencies in the package.json file while still being able to share certain logic/utility code between them. How ca ...

What is the best method for extracting and deleting specific variables from an array?

Consider the array below: $var_array = array( "length" => 5, "breadth" => 5, "color" => "blue", "size" => "medium", "shape" => "square"); If I want to extract specific variables by specifying their keys, how can it be done? For in ...

Tips for converting a QR code to data URL encoding to include in a JSON response

I created a nodejs function to encode a QR code and I want to return the result back to the caller function in order to create a JSON response. However, for some reason my code is not returning the data response. Can someone please help me figure out what& ...

Can you explain the role of [Yield, Await, In, Return] in EcmaScript syntax?

EcmaScript production rules often include the use of "modifiers" such as: [Yield, Await, In, Return] Here are a couple of examples: ArrayLiteral[Yield, Await]: ... ElementList[Yield, Await]: ... AssignmentExpression[+In, ?Yield, ?Await] I have look ...

Sliding and repositioning elements using jQuery

My goal is to build a simple slideshow for learning purposes. I want the list items to slide automatically to the left in a continuous loop. I've written some code that makes the slides move, but I'm struggling to set the correct position for ea ...

Ways to navigate back to the previous ajax request

I am currently working on a script that involves numerous ajax requests. The script is functioning well, and it features dynamic content that changes based on user selections or search inputs. I have been exploring how to manipulate the browser history in ...

The state of the checked value remains unaffected when using the Angular Material Checkbox

I am currently working on a list of elements and implementing a filter using pipes. The filter allows for multi-selection, so users can filter the list by more than one value at a time. To ensure that the filter persists even when the window is closed or t ...

Is it possible to trigger JavaScript after loading a page in jqTouch with AJAX?

Similar to the kitchensink demo, I have successfully used jqtouch to call external .html pages into the index page. One of these external pages contains a video that is being played using sublime player. Everything is pretty basic so far, but my challenge ...

Unable to view AngularJS expression output

I'm struggling to make an AngularJS expression appear on the screen. The content inside the curly braces is not displaying, even though I have checked the app with ng-inspector and confirmed that an object is being created using the ng-model directive ...

Tips for executing mocha tests using a reporter

Whenever I execute the command npm test, it displays this output: mocha ./tests/ --recursive --reporter mocha-junit-reporter All the tests are executed successfully. However, when I attempt to run mocha with ./tests/flickr/mytest --reporter junit-report ...

How do I automatically redirect a user after they login using react redux?

Currently, I am utilizing react redux in conjunction with Next.js. The goal is to redirect the user to a specific page if they meet certain requirements. I have implemented this logic within my useEffect function and it works as expected. However, upon r ...

The Vue warning indicates that there was a failed type check for the "value" prop. It was expecting an array but received a number with a value of 1

I am facing an issue with an input of type number where I need to restrict the user from entering a number greater than ten. Initially, everything was working fine until I decided to change the value to an array (from value: 1 to value: [1, 1]) After swit ...

Saving the current state of a member variable within an Angular 2 class

export class RSDLeadsComponent implements OnInit{ templateModel:RSDLeads = { "excludedRealStateDomains": [{"domain":""}], "leadAllocationConfigNotEditables": [{"attributeName":""}] }; oldResponse:any; constructor(private la ...

Inject the error into the Router in Express.js, utilizing Node.js

Is it possible to pass an error into an express.js Router? The express.js documentation does not provide a clear answer regarding passing errors in relation to middleware, routers, and error handling (I am unable to include links as I lack reputation). I ...

Performing an AJAX POST request in Laravel

I'm struggling with getting this ajax call to work, and I can't seem to figure out what's going wrong. View (html) <div class="col-sm-6 col-xs-3 pl0" style="margin-left: -5px;"> <button class="btn btn-primary visible-xs" n ...

When dynamically loading content with ajax, dynamic content fails to function properly

Currently, I am attempting to incorporate dynamic content loading after the user logs in by utilizing $.ajax. Here is how it's being done: $.ajax({ url: "functions.php", type: "GET", data: login_info, datatype: 'html', a ...

"Vercel encounters a read-only file system when trying to change file

Hey folks, I've been using vercel for deploying my project. There's an issue with one of the dependencies in my NextJS project, which is located inside the node_modules folder. This dependency is supposed to read and write files within its own di ...

Use jQuery to switch back and forth between two different sets of classes

I am attempting to switch between two different sets of classes using jQuery. My goal is to change from one custom icon to a font-awesome icon upon clicking an element. While I have been successful in changing a single class, I am facing challenges when tr ...