Remove Hardcoding and populate from json distinct values

Anonymous
2023-08-24T08:01:57.88+00:00
I want the CategoryDDL to  be populated from Burned.txt and not I want to remove the hardcoding
bicycling,conditioning,dancing... 


<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="Burned.aspx.cs" Inherits="Weight.Burned" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<head>
    <body style="background-color:powderblue;">
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
    <title>METS APP</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            setOption();
            $('#CategoryDDL').change(function () {
                setOption();
            });

            $('#Calculate').click(function Calculate() {
                var weigth = 0;
                var weightsel='';
                var time = 0;
                var perform = 'y';
                var MEts = $('#DescriptionDDL option:selected').val();
                weigth = $('#DropDownList1 option:selected').text() == 'lbs' ? $('#tb_Weight').val() / 2.2 : $('#tb_Weight').val();
                time = $('#DropDownList2 option:selected').text() == 'min' ? $('#tb_time').val() / 60 : $('#tb_time').val();
                                   
                if (weigth <30 || weigth > 200)
               {                      
                        alert("Weight out of range !");
                        perform = 'n';
               }

                if (time > 4)
               {
                    alert("Time out of range !");
                    perform = 'n';
               }

                if (perform == 'y') {
                    $('#result').val(weigth * time * MEts);
                }
            });
        });

        function setOption() {
            $('#DescriptionDDL').html('');
            var DefaultOption = '<option selected  disabled>' + "- Select -" + '</option>';
            $('#DescriptionDDL').append(DefaultOption);
            var Category = $('#CategoryDDL option:selected').text();
            if (Category != '- Select -') {
                $.getJSON('BURNED.json', function (data) {
                    for (var act of data.burned.ACTIVITY) {
                        if (act.CATEGORY.toLowerCase() == Category.toLowerCase()) {
                            var option = '<option value=' + act.METS + '>' + act.DESC + '</option>';
                            $('#DescriptionDDL').append(option);
                        }
                    }
                });
            }
        }
    </script>
</head>



<div>
    <h1>Burned Calories</h1>
     <br />
    Weight            : <br /><input name="tb_Weight" type="number"   id="tb_Weight"   /> 
    <select name="DropDownList1" id="DropDownList1">
       <!--   <option value="0">kg</option>!--> 
        <option value="0">lbs</option>
    </select>
    <br />
    Activity Time     : <br /><input name="tb_time" type="number" id="tb_time"  />  
    <select name="DropDownList2" id="DropDownList2">
        <option value="0">min</option>
        <option value="1">hr</option>
    </select>
    <br />
    Activity           : <br /><select name="CategoryDDL" id="CategoryDDL">
        <option selected="selected" value="- Select -" disabled="">- Select -</option>
        <option value="0">bicycling</option>
        <option value="1">conditioning exercise</option>
        <option value="2">dancing</option>
        <option value="3">fishing and hunting</option>
        <option value="4">inactivity quiet/light</option>
        <option value="5">lawn and garden</option>
        <option value="6">music playing</option>
        <option value="7">running</option>
        <option value="8">self care</option>
        <option value="9">sports</option>
        <option value="10">transportation</option>
        <option value="11">walking</option>
        <option value="12">water activities</option>
        <option value="13">winter activities</option>
    </select>
    <br />
    Description       : <br /><select id="DescriptionDDL"></select>
    <br />
    <button type="button" id="Calculate" value="Calculate">Calculate</button>
    <br />
    Result In Calories: <input name="result" type="text" id="result" readonly/>


</div>
  </body>
</asp:Content>
burned.txt.


{
  "burned": {
    "ACTIVITY": [
      {
        "CD": "1003",
        "METS": "14",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, mountain, uphill, vigorous"
      },
      {
        "CD": "1004",
        "METS": "16",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, mountain, competitive, racing"
      },
      {
        "CD": "1008",
        "METS": "8.5",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, BMX"
      },
      {
        "CD": "1009",
        "METS": "8.5",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, mountain, general"
      },
      {
        "CD": "1010",
        "METS": "4",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, <10 mph, leisure, to work or for pleasure (Taylor Code 115)"
      },
      {
        "CD": "1011",
        "METS": "6.8",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, to/from work, self selected pace"
      },
      {
        "CD": "1013",
        "METS": "5.8",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, on dirt or farm road, moderate pace"
      },
      {
        "CD": "1015",
        "METS": "7.5",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, general"
      },
      {
        "CD": "1018",
        "METS": "3.5",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, leisure, 5.5 mph"
      },
      {
        "CD": "1019",
        "METS": "5.8",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, leisure, 9.4 mph"
      },
      {
        "CD": "1020",
        "METS": "6.8",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, 10-11.9 mph, leisure, slow, light effort"
      },
      {
        "CD": "1030",
        "METS": "8",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, 12-13.9 mph, leisure, moderate effort"
      },
      {
        "CD": "1040",
        "METS": "10",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, 14-15.9 mph, racing or leisure, fast, vigorous effort"
      },
      {
        "CD": "1050",
        "METS": "12",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, 16-19 mph, racing/not drafting or > 19 mph drafting, very fast, racing general"
      },
      {
        "CD": "1060",
        "METS": "15.8",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, > 20 mph, racing, not drafting"
      },
      {
        "CD": "1065",
        "METS": "8.5",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, 12 mph, seated, hands on brake hoods or bar drops, 80 rpm"
      },
      {
        "CD": "1066",
        "METS": "9",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, 12 mph, standing, hands on brake hoods, 60 rpm"
      },
      {
        "CD": "1070",
        "METS": "5",
        "CATEGORY": "bicycling",
        "DESC": "unicycling"
      },
      {
        "CD": "2001",
        "METS": "2.3",
        "CATEGORY": "conditioning exercise",
        "DESC": "activity promoting video game (e.g., Wii Fit), light effort (e.g., balance, yoga)"
      },
      {
        "CD": "2003",
        "METS": "3.8",
        "CATEGORY": "conditioning exercise",
        "DESC": "activity promoting video game (e.g., Wii Fit), moderate effort (e.g., aerobic, resistance)"
      },
      {
        "CD": "2005",
        "METS": "7.2",
        "CATEGORY": "conditioning exercise",
        "DESC": "activity promoting video/arcade game (e.g., Exergaming, Dance Dance Revolution), vigorous effort"
      },
      {
        "CD": "2008",
        "METS": "5",
        "CATEGORY": "conditioning exercise",
        "DESC": "army type obstacle course exercise, boot camp training program�"
      },
      {
        "CD": "2010",
        "METS": "7",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, general"
      },
      {
        "CD": "2011",
        "METS": "3.5",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, 30-50 watts, very light to light effort"
      },
      {
        "CD": "2012",
        "METS": "6.8",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, 90-100 watts, moderate to vigorous effort"
      },
      {
        "CD": "2013",
        "METS": "8.8",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, 101-160 watts, vigorous effort"
      },
      {
        "CD": "2014",
        "METS": "11",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, 161-200 watts, vigorous effort"
      },
      {
        "CD": "2015",
        "METS": "14",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, 201-270 watts, very vigorous effort"
      },
      {
        "CD": "2017",
        "METS": "4.8",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, 51-89 watts, light-to-moderate effort�"
      },
      {
        "CD": "2019",
        "METS": "8.5",
        "CATEGORY": "conditioning exercise",
        "DESC": "bicycling, stationary, RPM/Spin bike class�"
      
 }     ]   } }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,502 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,746 Reputation points Microsoft Vendor
    2023-08-24T09:57:09.6066667+00:00

    Hi @Dotnet Engineer,

    I want the CategoryDDL to be populated from Burned.txt and not I want to remove the hardcoding bicycling,conditioning,dancing...

    You can use replace method to remove hardcoding.

    var str = act.DESC.replace(/\uFFFD/g, '');

    I don't quite understand what you want to do with removing the "bicycling","conditioning","dancing" fields. Do you want to delete all fields in the json file that contain these fields?

    From what I understand and testing your code, you want to delete the selected values contained in the CategoryDDL in the DescriptionDDL .

    Of course, if you think differently, you can use the method below, just replace Category.toLowerCase() with the field you want to remove.

    User's image

    You can use the code below.

     if (str.includes(Category.toLowerCase())){
         str = str.replace(Category.toLowerCase()+",", '')
     }    
    

    All Code

    function setOption() {
        $('#DescriptionDDL').html('');
        var DefaultOption = '<option selected  disabled>' + "- Select -" + '</option>';
        $('#DescriptionDDL').append(DefaultOption);
        var Category = $('#CategoryDDL option:selected').text();
        if (Category != '- Select -') {
            $.getJSON('BURNED.json', function (data) {
                for (var act of data.burned.ACTIVITY) {
                    if (act.CATEGORY.toLowerCase() == Category.toLowerCase()) {
                        var str = act.DESC.replace(/\uFFFD/g, '');
                        if (str.includes(Category.toLowerCase())) {
                            str = str.replace(Category.toLowerCase() + ",", '')
                        }
                        var option = '<option value=' + act.METS + '>' + str + '</option>';
    
                        $('#DescriptionDDL').append(option);
                    }
                }
            });
        }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.