A. B
A. B

Reputation: 373

find element in object typescript code

In these products I want to find only element "product_id":"4" and display in view. Can you help please?

> let products= [ {"product_id":"1", "product_name":"Product1",
> "description":"Product1", "default_price":50, "lastmoduserid":"31",
> "lastmoddtm":"2018-02-06T13:26:17.000Z", "active":1},
> {"product_id":"2", "product_name":"Product2",
> "description":"Product2", "default_price":60, "lastmoduserid":"31",
> "lastmoddtm":"2018-02-06T13:35:17.000Z", "active":1},
> {"product_id":"3", "product_name":"Product3",
> "description":"Product3", "default_price":80, "lastmoduserid":"31",
> "lastmoddtm":"2018-02-06T13:35:22.000Z", "active":1},
> {"product_id":"4", "product_name":"Product4",
> "description":"Product4", "default_price":100, "lastmoduserid":"31",
> "lastmoddtm":"2018-02-06T13:25:54.000Z", "active":1},
> {"product_id":"5", "product_name":"Product5",
> "description":"Product5", "default_price":120, "lastmoduserid":"31",
> "lastmoddtm":"2018-02-06T13:35:27.000Z", "active":1} ]}

Upvotes: 2

Views: 21603

Answers (5)

user7125929
user7125929

Reputation: 85

We can also use a filter. this.employees = this.employees.filter(x => x.id == this.employeeId)[0];

Upvotes: 1

santosh singh
santosh singh

Reputation: 28642

You can use array find method as shown below.

this.product = this.products.find(t=>t.product_id == "4");

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  products:any;
  product:any;
  json= `[ {"product_id":"1", "product_name":"Product1",
 "description":"Product1", "default_price":50, "lastmoduserid":"31",
 "lastmoddtm":"2018-02-06T13:26:17.000Z", "active":1},
 {"product_id":"2", "product_name":"Product2",
 "description":"Product2", "default_price":60, "lastmoduserid":"31",
 "lastmoddtm":"2018-02-06T13:35:17.000Z", "active":1},
 {"product_id":"3", "product_name":"Product3",
 "description":"Product3", "default_price":80, "lastmoduserid":"31",
 "lastmoddtm":"2018-02-06T13:35:22.000Z", "active":1},
 {"product_id":"4", "product_name":"Product4",
 "description":"Product4", "default_price":100, "lastmoduserid":"31",
 "lastmoddtm":"2018-02-06T13:25:54.000Z", "active":1},
 {"product_id":"5", "product_name":"Product5",
 "description":"Product5", "default_price":120, "lastmoduserid":"31",
 "lastmoddtm":"2018-02-06T13:35:27.000Z", "active":1} ]`

 constructor(){
   this.products=JSON.parse(this.json);
   this.product = this.products.find(t=>t.product_id == "4");
   console.log(this.product);
 }
}

DEMO

Upvotes: 3

Juan Garcia
Juan Garcia

Reputation: 59

you only need to use the function "find", if you execute this line of code you will obtain the object in "obtainedProduct" variable if not exists inside of the array then will return "obtainedProduct" variable with value undefined.

let obtainedProduct =  this.products.find(x => x.product_id == "4");

console.log(obtaniedProduct);

Upvotes: 1

Pramod Patil
Pramod Patil

Reputation: 2763

You can use lodash find method

console.log(_.find(products,{"product_id":"4"}));

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222552

You can use array.find()

this.product = this.products.find(t=>t.product_id == "4");

make sure to declare product

let product :any;

Upvotes: 2

Related Questions