如何检查特定的ID是否存在于json对象数组中

时间:2018-07-21 18:30:42

标签: javascript angular

我有一个Products数组,像这样

    Products[{product: {id: "Sss", name: "rttt", picture: "rrrrr"},
 quantity: 22},{product:{id:"tt",name:"qqq",picture:"iooo"}]
let item = Products.product.find((p) => p.productId === product.id);

我想在Angular 6中找到ID为tt的产品,怎么做?

1 个答案:

答案 0 :(得分:0)

您正在尝试将Array.find应用于单个对象(Products.product数组的第一项)Products。相反,您应该在find之上Products

var products = [{product :{id: "Sss", name: "rttt", picture: "rrrrr"}},{product:{id:"tt",name:"qqq",picture:"iooo"}}]
    
 var selProd = products.find(p => p.product.id = 'tt');
 
 console.log(selProd);