Passing a string within brackets to another string

balteo

My question relates to JS and custom elements.

I have the following code:

let product=this.getProduct(message['productname']);

What I am not clear about is that message is a string above...

What is the result of passing a string within brackets (i.e. ['productname']) to another string (i.e. message)?

As in here:

message['productname']

What is the name for this notation/syntax?

The full listing can be found below:

import { Component, TemplateRef, Renderer2, OnDestroy, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';


export class Product {
  public productname:string;
  public code: string;
  public cartprice: number;
  public price: number;
  public available: number;
  public qty: number;
  constructor(){

  }
}

@Component({
  selector: 'product-cart',
  templateUrl: './productcart.component.html',
  styleUrls: ['./productcart.component.css'],
  encapsulation: ViewEncapsulation.Emulated
})
export class ProductCartComponent implements OnInit, OnDestroy {

  public productlist : Product[]; 
  public totalprice=0;
  public ngOnInit(): void {
    this.productlist=[];
  }
  constructor(private renderer: Renderer2) {
  }
  @Input()
  set message(message: string) {
    this.processMessage(message);
  }
  get message(): string { return this._message; }
  _message: string;

  processMessage(message) {
    let product=this.getProduct(message['productname']);
    if(product !== undefined) {
      product.qty=product.qty + 1;
      product.cartprice=product.cartprice+message['price'];
      this.totalprice=this.totalprice+message['price'];
     } else if(message !== "" && message !== undefined) {
      product = new Product();
      product.qty=1;
      product.price=(message['price']!== undefined)?message['price']:0;
      product.productname=(message['productname'] !== undefined)?message['productname']:"";
      product.available=(message['available'] !== undefined)?message['available']:0;
      product.code=(message['code'] !== undefined)?message['code']:"";
      product.cartprice=(message['price'] !== undefined)?message['price']:0;
      this.productlist.push(product);
      this.totalprice=this.totalprice+product.price;
    }

  }

  getProduct(productname) : Product {
    let productObj=undefined;
    for(let product of this.productlist) {
      if(product.productname === productname) {
        productObj=product; 
        break;
      }
  }
  return productObj;
  }
  ngOnDestroy() {
  }
  increment(product) {
    if(product.qty >= 0 && product.qty < product.available) {
      product.qty =product.qty + 1;
      product.cartprice = product.cartprice + product.price;
      this.totalprice = this.totalprice + product.price;
      this.sendMessageToProductView(product);
    }
  }

  decrement(product) {
    if(product.qty > 0 && product.qty <= product.available) {
      product.qty =product.qty - 1;
      product.cartprice = product.cartprice - product.price;
      this.totalprice = this.totalprice - product.price;
      this.sendMessageToProductView(product);
    }
  }
  sendMessageToProductView(product) {
    const productviewele = document.querySelector('product-view');
    if(productviewele != null) {
      productviewele['message']=product;
    }
  }
}

Edit: I can confirm that message is of type object and not string. The setter parameter is mistyped as hinted by Jonas.

geekley

message['productname'] would be the same as message.productname, which will be undefined if message is a string. If message is an object, it will be the productnameproperty.

So the code:

product.productname=(message['productname'] !== undefined)?message['productname']:"";

seems to be getting the productname if message is an object and "" if it is a string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

If string is within another string

How to extract string match within brackets in R?

Split string by comma, but ignore commas within brackets

Extract number within square brackets from string

How to remove all string within brackets in a filename?

Splitting string on dots but not on dots within round brackets

Extract number within brackets at the end of a string - (YYYY)

how to get string outside brackets within brackets regex

Regex to capture string if other string present within brackets

Regex string between square brackets only if '.' is within string

Passing a string array to another function

iOS passing string to another app

Get the last occurrence of string within another string

Find if a string is contained within another string in R

SQL: Finding exact string within another string

Searching for part of string within another string in dataframe

Find a Custom Variable String within another String

Search for a string variable within another string variable

Validate the presence of a single string within another string

How to remove string starting with another string inside brackets?

Passing Python string literal as argument, surrounded with and without square brackets

Change all text color inside brackets within a string in Flutter

PHP preg_replace string – contents within brackets

In R, use stringr to extract values from a string that are within [] brackets

Count occurrences of a character within a string when outside square brackets

How can I extract a string within square brackets in xslt

grok filter (regex) to extract string within square brackets

Regex to match number in a string enclosed within brackets or Parenthesis

Regex to find all curly brackets within a quoted string