Add a new literal object to an array of objects!

-2

I would like to add a new item in my array of objects through a method, but I do not know how to do it.

public products: Array<Object> = [
  {prodName: 'product 1', prodElement: 'element 1', prodAttribute: 'Attribute 1', attrValue: 'value 1'},
];
    
asked by anonymous 21.09.2018 / 18:25

2 answers

1

products is getting an array, inside the array we have the .push property to add and .splice to remove, .push vc only passes an object or any type of primary that is in the array, splice vc passes the index and how second parameter the number of indexes to be removed.

products.splice(indexOf, 'itens a serem retirados a partir do index')
products.push({Objeto da forma como vc quiser inserir})

As JavaScript is poorly typed, it's okay to declare it as follows:

var products = [];

Creating your objects here.

products.push[{'nome':'Sérgio', 'idade':23, 'profissao':'programador angularjs}']
    
21.09.2018 / 18:55
0

push increments a new item to its array;

this.products.push({prodName: 'product 1'});

Maybe you have to declare it like this to work,

public products: any = [{prodName: 'product 1'}];

Next post code, n img

    
21.09.2018 / 18:48