根据子数组中的对象值过滤对象数组
Filter array of objects based on object value in subarray
我正在尝试根据 WooCommerce Rest API 中的对象子数组中的值过滤对象数组,以拉入我的 React 应用程序
这是数组的简化版本
const arrayExample =[
{
"id": 1,
"name": "Product Name 1",
"status": "publish",
"categories":[
{
"id": 34,
"name": "Category Name",
"slug": "category-name"
}
],
"acf":{
"data_throughput": "100"
}
},
{
"id": 2,
"name": "Product Name 2",
"status": "publish",
"categories":[
{
"id": 32,
"name": "Another Category Name",
"slug": "another-category-name"
},
{
"id": 35,
"name": "Other Category Name",
"slug": "other-category-name"
},
],
"acf":{
"data_throughput": "10"
}
},
{
"id": 3,
"name": "Product Name 3",
"status": "publish",
"categories":[
{
"id": 31,
"name": "New Category Name",
"slug": "new-category-name"
},
{
"id": 32,
"name": "Another Category Name",
"slug": "another-category-name"
},
{
"id": 34,
"name": "Category Name",
"slug": "category-name"
},
],
"acf":{
"data_throughput": "50"
}
},
]
我正在尝试获取类别名称为 "Category Name" 或 ID 为 34 的每个数组项。我尝试使用
执行过滤功能
const filterList = arrayExample.filter(info=>info.categories.name==="Category Name")
甚至
const filterList = arrayExample.filter(info=>info.categories.id===34)
但它们都return是空的。我不能做 info.categories[0] 因为它并不总是在第一位。
我正在为这些项目从我的网站调用 WooCommerce API,除了类别名称或 ID 之外,我找不到任何其他匹配的标识符来调用。我只是在接近这整件事吗?我需要在不同时间调用大约 5 个不同的类别。
您将类别作为一个数组,因此您需要通过索引访问它。
但是您试图在不指定任何索引的情况下直接访问。所以你可以简单地在你的过滤器中使用 some。
const arrayExample =[{"id": 1,"name": "Product Name 1","status": "publish","categories":[{"id": 34,"name": "Category Name","slug": "category-name"}],"acf":{"data_throughput": "100"}},{"id": 2,"name": "Product Name 2","status": "publish","categories":[{"id": 32,"name": "Another Category Name","slug": "another-category-name"},{"id": 35,"name": "Other Category Name","slug": "other-category-name"},],"acf":{"data_throughput": "10"}},{"id": 3,"name": "Product Name 3","status": "publish","categories":[{"id": 31,"name": "New Category Name","slug": "new-category-name"},{"id": 32,"name": "Another Category Name","slug": "another-category-name"},{"id": 34,"name": "Category Name","slug": "category-name"},],"acf":{"data_throughput": "50"}},]
let op = arrayExample.filter(({categories})=> categories.some(({id})=> id === 34))
console.log(op)
我正在尝试根据 WooCommerce Rest API 中的对象子数组中的值过滤对象数组,以拉入我的 React 应用程序
这是数组的简化版本
const arrayExample =[
{
"id": 1,
"name": "Product Name 1",
"status": "publish",
"categories":[
{
"id": 34,
"name": "Category Name",
"slug": "category-name"
}
],
"acf":{
"data_throughput": "100"
}
},
{
"id": 2,
"name": "Product Name 2",
"status": "publish",
"categories":[
{
"id": 32,
"name": "Another Category Name",
"slug": "another-category-name"
},
{
"id": 35,
"name": "Other Category Name",
"slug": "other-category-name"
},
],
"acf":{
"data_throughput": "10"
}
},
{
"id": 3,
"name": "Product Name 3",
"status": "publish",
"categories":[
{
"id": 31,
"name": "New Category Name",
"slug": "new-category-name"
},
{
"id": 32,
"name": "Another Category Name",
"slug": "another-category-name"
},
{
"id": 34,
"name": "Category Name",
"slug": "category-name"
},
],
"acf":{
"data_throughput": "50"
}
},
]
我正在尝试获取类别名称为 "Category Name" 或 ID 为 34 的每个数组项。我尝试使用
执行过滤功能const filterList = arrayExample.filter(info=>info.categories.name==="Category Name")
甚至
const filterList = arrayExample.filter(info=>info.categories.id===34)
但它们都return是空的。我不能做 info.categories[0] 因为它并不总是在第一位。
我正在为这些项目从我的网站调用 WooCommerce API,除了类别名称或 ID 之外,我找不到任何其他匹配的标识符来调用。我只是在接近这整件事吗?我需要在不同时间调用大约 5 个不同的类别。
您将类别作为一个数组,因此您需要通过索引访问它。 但是您试图在不指定任何索引的情况下直接访问。所以你可以简单地在你的过滤器中使用 some。
const arrayExample =[{"id": 1,"name": "Product Name 1","status": "publish","categories":[{"id": 34,"name": "Category Name","slug": "category-name"}],"acf":{"data_throughput": "100"}},{"id": 2,"name": "Product Name 2","status": "publish","categories":[{"id": 32,"name": "Another Category Name","slug": "another-category-name"},{"id": 35,"name": "Other Category Name","slug": "other-category-name"},],"acf":{"data_throughput": "10"}},{"id": 3,"name": "Product Name 3","status": "publish","categories":[{"id": 31,"name": "New Category Name","slug": "new-category-name"},{"id": 32,"name": "Another Category Name","slug": "another-category-name"},{"id": 34,"name": "Category Name","slug": "category-name"},],"acf":{"data_throughput": "50"}},]
let op = arrayExample.filter(({categories})=> categories.some(({id})=> id === 34))
console.log(op)