【发布时间】:2021-03-16 02:42:27
【问题描述】:
解释我的情况,我想过滤我的 API 的响应,它返回一个数组来提出这个问题,我采用这些测试数据
此 prodPrueba 模拟用户选择的产品和变体
let prodPrueba = [
{
id: "B801278",
variation: "B801278-18",
},
{
id: "PC002",
variation: "PC002",
},
];
id 是所选产品的代码,变体是用户为产品选择的变体,例如...鞋子是产品,尺寸 18 是变体 B801278-18
这是我后端的响应
let dataResponse = [
{
codProduct: "PC002",
created_at: "2021-01-19T11:28:06.000000Z",
desc: null,
html_description: null,
html_short_description: null,
id_kit: null,
kit: [],
manufacturer: null,
material: null,
name: "Kit de Limpeza",
id: "PC002",
status: "active",
tags: null,
theme: null,
title: null,
type: null,
variations:
[
{
product: "PC002",
cod: "U",
sku: "U",
product_sku: "PC002",
price: 96000,
}
]
},
{
codProduct: "ENG792015_9",
created_at: "2021-01-19T11:27:59.000000Z",
desc: null,
html_description: null,
html_short_description: null,
id_kit: null,
kit: [],
manufacturer: null,
material: null,
name: "Charm en Plata.",
id: "ENG792015_9",
status: "active",
tags: null,
theme: null,
title: null,
type: null,
variations: [
{
cod: "U",
product: "ENG792015_9",
product_sku: "ENG792015_9",
price: 415000,
sku: "U"
}
]
},
{
codProduct: "B801278",
created_at: "2021-01-19T11:27:30.000000Z",
desc: null,
html_description: null,
html_short_description: "Set de Regalo Brazalete en Plata Clip",
id_kit: null,
kit: [],
manufacturer: null,
material: null,
name: "Set de Regalo Brazalete en Plata, Clip",
id: "B801278",
status: "active",
tags: null,
theme: null,
title: null,
type: null,
variations: [
{
cod: "U",
price: 950000,
product: "B801278",
product_sku: "B801278",
sku: "U"
},
{
cod: "20",
price: 950000,
product: "B801278",
product_sku: "B801278-20",
sku: "U",
},
{
cod: "19",
price: 950000,
product: "B801278",
product_sku: "B801278-19",
sku: "U",
},
{
cod: "18",
price: 950000,
product: "B801278",
product_sku: "B801278-18",
sku: "U",
},
]
}
]
正如您在对象“prodPrueba”中看到的,我有 2 个 id 与返回 API 的产品一致,在 prodPrueba 中,我有一个属性调用“variation”,它与每个变量的“product_sku”一致产品就是返回 api。我想过滤响应以获取除我的 prodPrueba 中的所有变化之外的所有变化,我尝试使用 filter、map、forEach,但我不知道该怎么做,有什么建议吗?
【问题讨论】:
-
你能告诉我你想要什么输出吗?
-
我想在 codProduct 为“B801278”的产品中获取 dataResponse 所有变体,除了一个具有 product_sku“B801278-18”的产品,因为在 prodPrueba 中选择了该变体,如果我的英语太糟糕了
标签: javascript arrays angular