showing a single element from an array generated by ng-repeat

I am currently working on handling a large JSON file and displaying it using ng-repeat. However, I encountered a problem where I need to split the 'abilities' item and present it in a table format. I tried using a filter to divide the string into an array, but I am struggling to display a selected element from the resulting array.

Desired outcome: for the first object (CSS not included)

<abilities-block>
  <table>
    <tbody>
      <tr>
        <th>STR</th>
        <th>DEX</th>
        <th>CON</th>
        <th>INT</th>
        <th>WIS</th>
        <th>CHA</th>
      </tr>
      <tr>
        <td id="str">10</td>
        <td id="dex">14</td>
        <td id="con">10</td>
        <td id="int">11</td>
        <td id="wis">12</td>
        <td id="cha">11</td>
      </tr>
    </tbody>
  </table>
</abilities-block>

I have attached a codepen link and a lengthy snippet, apologies for the inconvenience.

Thank you for any assistance

var app = angular.module('app', []);

app.controller('controller', function($scope, $http) {
  $scope.allmonsters = {
    "monsters": [ {
    ... (omitted for brevity)
    }]
  }
});

angular.module('app')
  .filter('splitabilitiesfilter', function() {
    return function(input, scope) {
      var res = input.split(" ")
      return res;
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
  <MMsheets ng-repeat="x in allmonsters.monsters">
    <abilities-block>
      <table>
        <tbody>
          <tr>
            <th>STR</th>
            <th>DEX</th>
            <th>CON</th>
            <th>INT</th>
            <th>WIS</th>
            <th>CHA</th>
          </tr>
          <tr>
            <td id="str">{{x.abilities|splitabilitiesfilter:scope}}</td>
            <td id="dex">11 (+0)</td>
            <td id="con">13 (+1)</td>
            <td id="int">1 (–5)</td>
            <td id="wis">3 (–4)</td>
            <td id="cha">1 (–5)</td>
          </tr>
        </tbody>
      </table>
    </abilities-block>
  </mmsheets>
  <div>

Answer №1

Here is a suggestion that might meet your needs:

Consider adding an index to your filter:

angular.module('app').filter('splitabilitiesfilter', function() {
   return function(input, index, scope) {
     var res = input.split(" ")
     return res[index];
   };
});

Then, in your table:

<tr>
    <td id="str">{{x.abilities|splitabilitiesfilter:1:scope}}</td>
    <td id="dex">{{x.abilities|splitabilitiesfilter:3:scope}}</td>
    <td id="con">{{x.abilities|splitabilitiesfilter:5:scope}}</td>
    <td id="int">{{x.abilities|splitabilitiesfilter:7:scope}}</td>
    <td id="wis">{{x.abilities|splitabilitiesfilter:9:scope}}</td>
    <td id="cha">{{x.abilities|splitabilitiesfilter:11:scope}}</td>
</tr>

Hopefully, this solution is helpful to you.

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

Steps to remove OTP from dynamodb after a specified time period (2 minutes)

Recently, I have been utilizing the setImmediate Timeout function to call the deleteOTP function with details such as the userId of the OTP to be deleted. However, I am encountering challenges when passing the argument (userId) to the deleteOTP function ...

Obtain specific fields from a multidimensional array object using lodash

My dilemma involves working with an object that has the following structure: var data = [ { "inputDate":"2017-11-25T00:00:00.000Z", "billingCycle":6, "total":1 },{ "inputDate":"2017-11-28T00:00:00.000Z", "bi ...

Tips for effectively loading data of a specific user or event into a bootstrap modal in Laravel using AJAX

I'm having some trouble loading Event data into my bootstrap modal in Laravel. This is all new to me as I'm just getting started with AJAX. Can someone please take a look at what I've done so far? Modal Template <div class="modal fa ...

The background failed to display (potentially due to a hovering function)

I encountered an issue with a div that has a background image. Upon loading the page, the background image fails to display initially. However, when I hover over the div and then move my mouse elsewhere (due to a specific function described below), the bac ...

jQuery: Eliminating Parent Elements

Currently, I am developing a small web application that allows users to create lists which are then formatted and emailed to them. However, I am facing difficulties in implementing a method that will allow users to delete list items without clearing the en ...

Is it possible to compile a TypeScript file with webpack independently of a Vue application?

In my Vue 3 + Typescript app, using `npm run build` compiles the app into the `dist` folder for deployment. I have a web worker typescript file that I want to compile separately so it ends up in the root of the `dist` folder as `worker.js`. Here's wha ...

Dynamic Bootstrap Popover: Creating interactive popups by dynamically attaching events to buttons

I am looking to implement the Bootstrap Popover module to create a confirmation dialog for a delete action. When a <button> is clicked, instead of immediately executing a function, I want a popup to appear allowing the user to either confirm or dismi ...

Learn how to transform the html:options collection into html:optionsCollection

What is the best method to transform this code snippet: <html:options collection='catList' property='catId' labelProperty='catName'> using the <html:optionsCollection> tag? ...

JavaScript Paint Brush Tool Powered by HTML5

I am looking to create a smooth and clean opacity brush. Here is an example of the drawing line I want: This is the second picture I managed to achieve: When I move the cursor quickly, there are fewer circles in the drawing line. ...

Is your React conditional rendering malfunctioning due to state issues?

I am attempting to create a component that will only be displayed after clicking on a search button. Below is the current code that I have: Update After making some changes, I am now encountering this error: Error: ERROR in /home/holborn/Documents/Work ...

When the jQuery Div is moved to the right, it gradually shrinks in size, yet remains unchanged when

I have been making updates to this page, which you can view here. When you select "Let's Get Started" and then proceed with the right arrows, the divs smoothly move to the left without shrinking. However, when clicking on the back or left arrows, the ...

What sets Express.js apart from koa2.js in handling asynchronous functions?

I've encountered a situation where I had to set up the router using Express, and it was functioning correctly with the following code: router.get('/',(req,res)=>{ queries.getAll().then(stickers=>{ res.json(stickers) }) ...

What is the best way to modify the ID of a duplicated element?

I have been working on creating a drag and drop editor using react-smooth-dnd. The setup involves two containers: a toolbar with elements and an editor where the elements can be dragged and dropped. Each element in the toolbar has the following structure: ...

Using Sequelize and Express API for verification in the controller is an essential component of building

I'm currently working on building the API for my web app, utilizing Sequelize and Express. I have set up models with Sequelize and am in the process of developing controllers and endpoints. My main query is: Should I perform data validation checks be ...

Is Dealing with Multiple AJAX Requests a Pain?

Currently, I am utilizing AJAX to retrieve a list of active streams from TwitchTV along with their viewers, updating every second. Sometimes the stream list can become quite long, so my plan is to divide the AJAX requests into 2 or 3 parts: 1) Obtain Numb ...

The parameters 'event' and 'event' are not compatible with each other

I'm currently working on a page that involves submitting a form: import React from 'react'; import Form from 'react-bootstrap/Form'; import { useSignIn } from '../../hooks/Auth/useSignIn'; import { useHistory } from &apos ...

Send a request for a multidimensional array to a PHP file using AJAX

I am in need of an array containing subarrays. Within my ProcessWire page, there are pages and subpages with various fields. I have organized this information into an array of arrays as shown below. <?php $allarticles = $pages->find("template=a ...

Is there a way to refresh the array in Swift 3 once it has been exhausted or fully populated?

Is there a way to reset the game or words once it's finished? I'm a beginner and finding it difficult to solve this issue! var array = [ "word 1", "word 2", "word 3", "word 4", ...

The functionality of Bootstrap's JavaScript is dependent on the presence of jQuery - Reactjs framework

When attempting to combine bootstrap with reactjs, I encountered an issue with importing Bootstrap.js It seems that Bootstrap's JavaScript requires jQuery Below are some of the import statements I have tried: import $ from 'jquery'; imp ...

What is the process of integrating an ejs view engine with express on Netlify?

Need help configuring the ejs view engine with netlify I attempted to set app.set('view engine', 'ejs'), but didn't see any results. const express = require('express'); const path = require('path'); const serv ...