Posts

Showing posts from September, 2020

how to delete selected objects from array by key value. javascript

//main array list arrayList = [{'id':1,'name':'salman',age:'22'},{'id':2,'name':'Adam',age:'21'},{'id':3,'name':'John',age:'23'}]; //list of object ids to be deleted idList [id:1,id:3]; //delete method idList.forEach(f => arrayList.splice(arrayList.findIndex(e => e.id === f.id),1));  console.log(arrayList); ----Output----  [{'id':2,'name':'Adam',age:'21'}];

how to save images with angular nodejs

 There are two ways to save images. 1) store image in database by converting it to binary format. 2) copy image in your project folder then save image path in table. ------- 1st way with front end angular------ pass uploaded file in "handleFileInput" function.  saveImage ( files :  FileList ) {    var   file : File  =  files . item ( 0 );    var   myReader : FileReader  =  new   FileReader ();    myReader . onloadend  = ( e )  =>  {      this . image  =  myReader . result ;   }    myReader . readAsDataURL ( file );    //convert to base64 ends } now you will get  binary format image in this.image variable. save this converted image directly to database through API. ------2nd way with api nodejs------ we can use "multer" to copy file in project folder. const   multer  =  require ( 'multer' ); var   storage  =  multer . diskStorage ({    destination :   './images' ,    filename :  ( req ,  file ,  cb )  =>  {        cb ( null ,  Date . now () + 

How to filter objects from array javascript

 var products = [{"_id":1,"name":"product1 ","description":"good product"}, {"_id":2,"name":"product2 ","description":"good product"}, {"_id":3,"name":"product3 ","description":"good product"}, {"_id":4,"name":"product4 ","description":"good product"}, {"_id":5,"name":"product5 ","description":"good product"}]  var newArray = products.filter(function (el) {                 return el._id > 2;                }); -------Output------ [{"_id":3,"name":"product3 ","description":"good product"}, {"_id":4,"name":"product4 ","description":"good product"}, {"_id":5,"name":"product5 ","description":"good p