How to create an array (and perform push, remove and show its elements) in Angular 2?

How to create an array (and perform push, remove and show its elements) in Angular 2?

Content Index :

How to create an array (and perform push, remove and show its elements) in Angular 2?
Tag : typescript , By : vferman
Date : November 27 2020, 11:01 PM

around this issue you can use simple javascript array function to add and remove item in array. I have an Sample example in which on checkbox click I add and remove item from array like:
Html Code :
<div *ngFor="let tvalue of typeArr; let i=index;" class="checkbox">
  <label class="text-capitalize">
  <input formControlName="value" type="checkbox" class="styled"
    {{ tvalue }}
   public typeArr:Array<any> = ['angular2','Php','MySql','Java'];    
   public filterlist;

   addtoarray(key, value) {
     if (typeof this.filterlist == 'undefined') {  //check list 
       this.filterlist = {};
    let dimensions = this.filterlist;
    dimensions[key] = dimensions[key] || [];  //initialize array

    let i = dimensions[key].indexOf(value);
    if (i != -1) {
     dimensions[key].splice(i, 1);   //remove array element
    else {
     dimensions[key].push(value);    //insert array element

    if (dimensions[key].length == 0) {
     delete dimensions[key];        //delete array
   this.filterlist = dimensions;     

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to push array elements to an array if _id of documents already exists and create new document if _id doesn't exist?

Tag : node.js , By : brennen
Date : March 29 2020, 07:55 AM
this one helps. I am using Node.js, mongoose, mongodb, express and angular. I am saving replies for a survey in a mongoose model. Many people will submit replies for a particular survey. When the first person submits a reply for a survey, I want to create a new document for that survey. When the second, third.... so on people submit replies for the same survey, I want to add array elements only to the replies array in the following schema. , pseudo untested code.
    MCQReply.findOne({_id : the id}, function(err,doc){
        if(err) console.log(err);
        // if the survey isn't there `doc` will be null then do your save
          new MCQReply({

           _id : '123',
            surveyname: 'sample',
            replies :[{
              replierId : 'R001',
              answers : [{
                questionId : 'A001',
                answer : 'answer'

            }).save(function(err, doc){
              if(err) res.json(err);
              req.flash('success_msg', 'User registered to Database');  

            //you get mongoose document
            doc.replies.push({replierId : "R001", answer : [{questionId : "A001" ... whatever else you want}]})

            //dont forget to save
            doc.save(do error check)

var MCQReplySchema = new Schema({ 

    _id : String,
    surveyname: String,
    replies :[{
        replierId : String,
        answers : [{
            questionId : String,
            answer : String

    }, {strict : false});

How to push array elements into a library element type in angular

Tag : angular , By : Naveen
Date : March 29 2020, 07:55 AM
This might help you You're attempting to push an array of strings, not an object, assuming the type of Track is
interface Track {
   title: string;
   link: string
  { title: song.title, link: song.link }

How to push elements of an array if elements are matching in another array in angular?

Tag : angular , By : yew tree
Date : March 29 2020, 07:55 AM
it fixes the issue You can simply use below code and achieve he desired outcome, no need to use any library.
array1.forEach(elements => {
  let a = {};
    a[elem.header] = elements[elem.field];

How to push JSON records into an empty array as individual elements [Angular]

Tag : javascript , By : Lathentar
Date : March 29 2020, 07:55 AM
I wish this help you The problem is you are pushing an array into array. You should pushindividual elements and concat arrays.
It should be either:
this.users = json;
this.users.push(...json); // Destructure the array first

Push elements of an array to different keys using Javascript & Angular JS

Tag : javascript , By : stan
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Type guard with complement in false branch
  • Manually widen type
  • How do I type for arrays with ordered types of unknown length?
  • Typescript switch case confusing compiler
  • Using externally defined component with Vue and TypeScript class decorator syntax
  • typescript multiple generic cannot infered properly
  • How can I find where TS type is defined in my app?
  • Module.exports with es6 import for webpack.config.ts in typescript
  • TSLint: ordered-imports configuration
  • Protractor typescript Error: WebDriverError: java.net.ConnectException: Connection refused: connect while selecting valu
  • How can I write extension for generic class in typescript as getter
  • How to properly define global enum in Typescript
  • TestCafe - Checking if the hyperlink is working - redirection to an external email provider
  • Typescript, from us currency to es currency
  • TypeScript: Declaring a callable type as a class method type
  • Retrieve current value of an observable
  • Using the keys of an object literal as a Typescript type?
  • Cant get variable in data
  • TypeScript inferencing with process.exit
  • Compare two interfaces at runtime
  • Understanding .tsconfig file
  • Is function () { this.array = [newElem, ...array] } pure?
  • How can I ignore operating system font in react-native?
  • rxjs: Map adding field
  • How to use Mocha and Jest with TypeScript without conflicts?
  • Vue 2 + TypeScript: Avoid mutating Prop directly - in a class based component with vue-property-decorator
  • Use an object as a map's key in the value
  • How to allow partial TypeScript types when mocking functions - Jest, TypeScript
  • How to declare default-function for a class?
  • Deriving TypeScript interface from object literal
  • Accessing generics from inherited type
  • ServiceStack - Cannot get headers from in Typescript client ResponseFilter
  • Object.entries(qs).forEach(...) - resolve lambda ignoring type annotations
  • Typescript: Create object with same keys but different values
  • Defining a list of tuples with optional first and last elements
  • Error Message: "An interface can only extend an object type or intersection of object types with statically known m
  • Nuxt custom plugin exports is not defined
  • TypeGraphQL use TypeORM's findAndCount method
  • Typescript interface with keys of Generic
  • Need to click twice in KeyboardAvoidingView even when keyboardshouldpersisttaps is always
  • Typescript type safe HTTP post not really type safe
  • Typescript: Define type as any but function
  • How to map DTO using interfaces in typescript?
  • How to make a generic type argument required in typescript?
  • Get value of Observable<SomeObject> in an object
  • Typescript generics: Howto map array entries to object keys
  • How to restrict method argument to index number of tuple type?
  • How can we use Typescript with CodeceptJS for testing framework?
  • How to enable "error prevention only" in TSLint? (Disable style checks, etc.)
  • Working with typescript non-discriminated unions
  • how to limit keys of an object to a list of set strings in typescript
  • How to type a function so that its return type matches that of an array that is being returned?
  • Typescript not building in Container-based Azure Function App
  • Redeclare destructured variables in Typescript
  • Object literal which implements an interface which in turn uses a generic method
  • How to set a Type Literal on a widened type parameter (or prevent type widening)?
  • Correct type for a function that sets individual properties of an object?
  • What is bivariant parameter? ~ TypeScript
  • Typescript: “not assignable to parameter of type never” error
  • Typescript: prevent assignment of object with more properties than is specified in target interface
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com