0

I am reading a list that has the following structure:

export interface ISaleEntity {
    id: number;
    dateCreated: Date,
    amount:number,
    type:string,
    description:string
  }

My api is returning the following data:

payments: Array(2) 0: {Id: 1, Type: "DEBIT", Description: "Sale 1", Amount: 5000, DateCreated: "06/18/2018 00:00:00"} 1: {Id: 2, Type: "CREDIT", Description: "Sale1", Amount: 4200, DateCreated: "06/20/2018 00:00:00"}

Since I am using transcript, I do

 const payments: ISaleEntity [] = response.data.payments;



private renderData(payments: ISaleEntity[]) {
    return (
      <div>
        {payments.length}
        {payments.forEach(element => 
       // tslint:disable-next-line:no-console
       console.log("element" + element)

        // <span>{element.description}</span>
        )}
      </div>
    );
  }

In console, element is [object Object].

How can I read the loop through JSON object properties?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79

2 Answers2

1

Just pass it as another argument to console.log:

console.log("element", element);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

var elements=[{Id: 1, Type: "DEBIT", Description: "Sale 1", Amount: 5000, DateCreated: "06/18/2018 00:00:00"} ,{Id: 2, Type: "CREDIT", Description: "Sale1", Amount: 4200, DateCreated: "06/20/2018 00:00:00"}]

elements.forEach(function(elem){
  console.log(elem);
  console.log(elem.Description)
})

console.log(elements[0].Description)

If you want to concatenate with string just use

console.log("element" + JSON.stringify(element)).

+ element coerces the object element into a string, which is just [object Object]

console.log is an overloaded function that accepts a list of parameters that are either passed by copy (string|number|boolean) or by reference (everything else).

manikant gautam
  • 3,521
  • 1
  • 17
  • 27