When a user clicks some items on my web page, I want to store the item details in an array of objects like this:
[{ "color": "blue", "shape": "square" } , { "color": "red", "shape": "triangle" }]
However, the following code throws an error when I try to push(tempObj):
export class MyComponent implements OnInit {
itemsClicked: {color: string, shape: string}[] = [];
ngOnInit(): any { }
onItemClick(color, shape) {
let tempObj = {};
tempObj["color"] = color;
tempObj["shape"] = shape;
this.itemsClicked.push(tempObj);
}
}
Error:
Argument of type '{}' is not assignable to parameter of type '{ color: string; shape: string; }'.
tempObjto be the same as{color: string, shape: string}.colourandcolorand not the same spelling for variable names.