Search This Blog

Tuesday, June 13, 2023

Validating to select in sequencial order using angular


    <input type="checkbox" (change) = "handleSelectTaskItem($event, taskItem)"
[checked]="taskItem.isCompleted" class="custom-control-input" />




   public handleSelectTaskItem(event, taskItem:any) {

        const currentIndex = this.full.task.findIndex((task) => task.id === taskItem.id);

        for (let i = 0; i < currentIndex; i++) {

          const previousTask = this.full.task[i];




          if (!previousTask.isCompleted) {
            event.currentTarget.checked = false
            return;
          }
        }


        if(taskItem.isCompleted === false)
        {
          taskItem.isCompleted = true
          event.currentTarget.checked = true
          return
        }
        else
        {
          taskItem.isCompleted = false
          event.currentTarget.checked = false
        }

    }

Friday, April 28, 2023

Angular- validating the form field using adding 4 digits number

 To prevent the letter "e" from being entered as a valid character in an input field of type "number", you can use the "onkeydown" event and check if the "e" key is pressed. If the "e" key is pressed, you can prevent the default behavior of the input field, which is to allow the "e" key to be entered as part of a scientific notation number.

Here's an example of how to prevent the letter "e" from being entered in an input field of type "number" using the "onkeydown" event:

<input type="number" formControlName="frequency" class="form-control" [readOnly]="!isEditable"  inputmode="numeric" pattern="[0-9]*" onKeyPress="if(this.value.length === 4 || event.key === 'e') return false;" />

Wednesday, November 30, 2022

How to create a nested array

Step1:

To create nested array

Step2:

Excepting the Output of Nested Array

{

     requestDate  : "16-03-2023",

    copyshPdRec  : 1,

    copyshcomment  : 2

listOfSubdistricts: [{

    requestDate  : "16-03-2023",

    copyshPdRec  : 1,

    copyshcomment  : 2

      },

{

    requestDate  : "16-03-2023",

    copyshPdRec  : 1,

    copyshcomment  : 2

      }]

}


   copyPDValueDCFT(nValue?:any){
if(this.selectedProductCat === 'DCFT')      {
     
     
    this.copyPd = false;

     
 
  }

  this.copyPdValues = [];
 
console.log(nValue);

   nValue?.forEach((element, index) => {


    console.log(index);

     var roles=this.tokenStorage.getUserRole();
   
     let objRole:any={};

     objRole.type = roles.indexOf("3")!=-1 ? "SH" : roles.indexOf("5")!=-1 ? "ZH" : "RH";
   
 
     let  copyObjpdValues:any = {}
 
     if(objRole.type =="SH")
     {
       copyObjpdValues.requestDate = element.requestDate;
 
       copyObjpdValues.copyshPdRec = element.shPdRec;
   
       copyObjpdValues.copyshcomment = element.shComments;

     }
     if(objRole.type =="ZH")
     {
       copyObjpdValues.zh_effective_date = element.zh_effective_date;
 
       copyObjpdValues.copyzhPdRec = element.zhPdRec;  
   
       copyObjpdValues.copyzhcomment = element.zhComments;
     }
     if(objRole.type =="RH")
     {
       copyObjpdValues.lastapprovedDate = element.lastApprovedDate;
 
       copyObjpdValues.copyrhPdRec = element.lrsPdRec;
   

       copyObjpdValues.copyrhcomment = element.lrshComments;

     }
     
   
   
   

      //element?.listOfSubdistricts?.forEach(listelements => {

 copyObjpdValues.listOfSubdistricts =[];


 for(var i=0; i < element?.listOfSubdistricts?.length; i++)
{



        let subObj:any ={}
           
        if(objRole.type =="SH")
        {
         
         
           subObj['SHsubrequestDate'] = element?.listOfSubdistricts[i].requestDate;
   
           subObj['SHsubshPdRec'] = element?.listOfSubdistricts[i].shPdRec;
     
           subObj['SHsubshcomment'] =  element?.listOfSubdistricts[i].shComments;

           
        }
 

        if(objRole.type =="ZH")
        {
          subObj['ZHsubzh_effective_date'] = element?.listOfSubdistricts[i].zh_effective_date;
   
          subObj['ZHsubzhPdRec'] = element?.listOfSubdistricts[i].zhPdRec;  
       
          subObj['ZHsubcopyzhcomment'] = element?.listOfSubdistricts[i].zhComments;
        }



        if(objRole.type =="RH")
        {
          subObj['RHsublastapprovedDate'] = element?.listOfSubdistricts[i].lastApprovedDate;
   
          subObj['RHsubrhPdRec'] = element?.listOfSubdistricts[i].lrsPdRec;
     
   
          subObj['RHsubrhcomment'] = element?.listOfSubdistricts[i].lrshComments;
   
        }
     
        copyObjpdValues.listOfSubdistricts.push(subObj);    
       
      }

     this.copyPdValues.push(copyObjpdValues);

    });


}

Wednesday, September 21, 2022

Array iterators and working through nested objects and arrays - https://medium.com/@vshan0816/array-iterators-and-working-through-nested-objects-and-arrays-81e3f762e2ad

Array iterators and working through nested objects and arrays

const players = () => [...gameObject().home.players, ...gameObject().away.players]
const player = (name) => players().find(player => player.playerName === name)
const numPointsScored = (name) => {
return player(name).points
}
Which player has the most points? Call the function mostPointsScored.
Which team has the most points? Call the function winningTeam.
Which player has the longest name? Call the function playerWithLongestName.

Tricks to Loop Through an Array of Objects in JavaScript - https://www.webmound.com/javascript-loop-array-of-objects/

 We loop through an array of objects in JavaScript for many reasons. For example, you can iterate and find an object from an array of objects using JavaScript based on some condition.

The forEach() method can be used to loop through the array of objects in JavaScript. It gives access to each object present inside an array. There are other statements like for and for…of in JavaScript that can do the same thing.

You don't need any special methods or techniques to iterate over an array of objects. You can use the regular JavaScript array methods.

const people = [
    {
        name: 'john',
        age: 35,
    },
    {
        name: 'david',
        age: 27,
    },
    {
        name: 'kavin',
        age: 35,
    },
    {
        name: 'william',
        age: 43,
    },
    {
        name: 'jimmie',
        age: 21,
    },
];

I will use this "people" array to give you examples though out this article.

You will see 3 ways to loop over this array of objects. You will also learn when you should choose which one at the end of the post.

Tricks to Loop Through an Array of Objects in JavaScript

Use forEach() to Loop Over an Array of Objects

The forEach() method is one of the easiest ways to iterate over an array of objects. This method accepts a callback function and executes that function for each item in the array.

people.forEach((person) => {
    for (const key in person) {
        console.log(`${key}: ${person[key]}`);
    }
});

Output:

name: john
age: 35
name: david
age: 27
name: kavin
age: 35
name: william
age: 43
name: jimmie
age: 21

You also have access to each object and its index as the function parameters. I am looping over the people array and getting every object in the person parameter.

Now, you can use this object however you want. I am using the for...in statement to loop through the object properties in JavaScript.

Use for Loop to Iterate Over an Array of Objects

JavaScript has a traditional the for statement that can create a loop and give you access to all items inside an array. Therefore, you can use this statement to loop over an array of objects.

for (let i = 0; i < people.length; i++) {
    console.log(people[i].name);
}

Output:

john
david
kavin
william
jimmie

It is very easy to the for statement in JavaScript. You start from index 0 and continue the loop until the index is less than the length of the people array.

Inside the loop, you have the current index value that can give you the current object. I am printing the name of each person from the array.

But you can perform any operation you want. Like you can shuffle a JavaScript array by using this technique.

Use for…of to Loop Through an Array of Objects

The for...of statement is used to execute a loop over a JavaScript array. It is similar to the traditional for statement but has an easy syntax.

It will loop through an array of objects and give you access to the objects directly instead of the index.

for (const person of people) {
    console.log(`${person.name} is ${person.age} years old.`);
}

Output:

john is 35 years old.
david is 27 years old.
kavin is 35 years old.
william is 43 years old.
jimmie is 21 years old.

I am creating the loop over the people array and getting each object in the person variable. Now, you can check the objects and remove items from the array very easily.

Conclusion

You have seen 3 ways to iterate over an array of objects. The forEach() method is very easy to use because of its simple syntax. But if you consider the performance then it may not be the best choice.

Note: The traditional for statement is much faster than the forEach() method in JavaScript. If your application needs performace optimization consider using for loop instead of the forEach() method.

Another advantage of using for and for...of is that you can apply the break and continue statements of JavaScript for some conditions.

But as I have seen so far, it really doesn't matter 99% of the time. People use the forEach() method in most of their applications. So if you are also using this method, it's not the end of the world.

So, you can apply any of these techniques to loop through an array of objects in JavaScript for your application.

Validating to select in sequencial order using angular

    < input type = "checkbox" (change) = "handleSelectTaskItem($event, taskItem)" [checked] = " taskItem . i...