我需要过滤和映射数组项和每个项的子数组项,同时判断要过滤的项是否具有满足特定条件的特定属性.

基于数组方法的过滤和映射嵌套数组项的好方法是什么?

Required Properties

"productName", "productCategory", "content", "id"

如果productImages中的status不是LinkedExisting

Sample Data

[
  {
    "productName": null,
    "productCategory": null,
    "productImages": [
      {
        "content": "1",
        "id": null,
        "data": null,
        "file": null,
        "url": null,
        "status": "Existing"
      },
      {
        "content": "",
        "id": "234",
        "data": "test data",
        "file": "test file",
        "url": null,
        "status": "Existing"
      }
    ]
  },
  {
    "productName": null,
    "productCategory": "hello category",
    "productImages": [
      {
        "content": "1",
        "id": null,
        "data": null,
        "file": null,
        "url": null,
          "status": "Existing"
      },
      {
        "content": "",
        "id": "234",
        "data": "test data",
        "file": "test file",
        "url": null,
          "status": "Existing"
      }
    ]
  },
  {
    "productName": "test product",
    "productCategory": "test category",
    "productImages": [
      {
        "content": "1",
        "id": "123",
        "data": "test data",
        "file": "test file",
        "url": null,
        "status": "Linked"
      },
      {
        "content": "2",
        "id": "234",
        "data": "test data",
        "file": "test file",
        "url": "test",
        "status": "Linked"
      }
    ]
  },
  {
    "productName": "new product",
    "productCategory": "new category",
    "productImages": [
      {
        "content": "2",
        "id": "32332",
        "data": "test data",
        "file": "test file",
        "url": "test",
        "status": "new"
      }
    ]
  }
]

Expected Output

[
  {
    "productIndex": 3,
    "name": "new product",
    "category": "new category",
    "filteredImages": [
      {
        "newcontent": "2",
        "id": "32332",
      },
    ]
  },
]

Code

const filteredProducts = products.filter((element) => element.productName && element.productCategory);

推荐答案

你可以用一个for-of圈来做到这一点:

const incomplete_items = [];
const new_data = []
for (const [index, item] of data.entries()) {
  if(item.productName && item.productCategory && item.productImages.every((image) => image.id && image.content && !['Existing', 'Linked'].includes(image.status))){
    new_data.push({
      productIndex: index,
      name: item.productName,
      category: item.productCategory,
      filteredImages: item.productImages.map((image) => ({ newcontent: image.content, id: image.id,  }))
    })
  } else {
    incomplete_items.push(index);
  }
}

Working example

const data = [
  {
    "productName": null,
    "productCategory": null,
    "productImages": [
      { "content": "1",  "id": null, "data": null, "file": null, "url": null, "status": "Existing" },
      { "content": "","id": "234","data": "test data", "file": "test file", "url": null, "status": "Existing" }
    ]
  },
  {
    "productName": null,
    "productCategory": "hello category",
    "productImages": [
      { "content": "1", "id": null,"data": null, "file": null, "url": null, "status": "Existing"},
      { "content": "", "id": "234","data": "test data","file": "test file", "url": null, "status": "Existing" }
    ]
  },
  {
    "productName": "test product",
    "productCategory": "test category",
    "productImages": [ 
      { "content": "1", "id": "123", "data": "test data", "file": "test file", "url": null, "status": "Linked" },
      { "content": "2", "id": "234", "data": "test data", "file": "test file", "url": "test","status": "Linked" }
    ]
  },
  {
    "productName": "new product",
    "productCategory": "new category",
    "productImages": [
      { "content": "2","id": "32332", "data": "test data", "file": "test file", "url": "test", "status": "new" }
    ]
  }
]

const incomplete_items = [];
const new_data = []
for (const [index, item] of data.entries()) {
  if(item.productName && item.productCategory && item.productImages.every((image) => image.id && image.content && !['Existing', 'Linked'].includes(image.status))){
    new_data.push({
      productIndex: index,
      name: item.productName,
      category: item.productCategory,
      filteredImages: item.productImages.map((image) => ({ newcontent: image.content, id: image.id,  }))
    })
  } else {
    incomplete_items.push(index);
  }
}

if(incomplete_items.length) console.log(`Items ${incomplete_items} were missing required properties.`);
console.log(new_data);

Javascript相关问答推荐

在JS中转换mysql UTC时间字段?

我的YouTube视频没有以html形式显示,以获取免费加密信号

vanillajs-datepicker未设置输入值,日期单击时未触发更改事件

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

colored颜色 检测JS,平均图像 colored颜色 检测JS

在我的html表单中的用户输入没有被传送到我的google表单中

如何迭代叔父元素的子HTML元素

对网格项目进行垂直排序不起作用

在Java中寻找三次Bezier曲线上的点及其Angular

Webpack在导入前混淆文件名

在css中放置所需 colored颜色 以填充图像的透明区域

如何 for each 输入动态设置输入变更值

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

在不删除代码的情况下禁用Java弹出功能WordPress

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

每次重新呈现时调用useState initialValue函数

是否可以将Select()和Sample()与Mongoose结合使用?

如何使用画布在另一个内部绘制一个较小但相同的形状,同时保持恒定的边界厚度?

在HTML5画布上下文中使用putImageData时,重载解析失败

如何在每隔2分钟刷新OKTA令牌后停止页面刷新