【问题标题】:Filter an array of objects with a second array with multiple values使用具有多个值的第二个数组过滤对象数组
【发布时间】:2020-04-09 15:51:33
【问题描述】:

我正在尝试编写一个函数来获取“父”数组中的第一个对象,拉出子字段(位于该数组中)并使用该字段过滤名为“子”的第二个对象。

我想从父对象的子字段中获取子对象的所有相关记录。

预期输出

  child: [
    {
      **id: 1,**
      name: 'Jimmy Yukka',
    },
    {
      **id: 2,**
      name: 'Up North',
    }

输入

 Parent: [
    {
      **id: 1,**
      name: 'Melbourne Bands',
      **child: [1, 2]**
    }

我有以下数据

 Parent: [
    {
      **id: 1,**
      name: 'Melbourne Bands',
      **child: [1, 2]**
    },
    {
      id: 2,
      name: 'Sydney Bands',
      child: [3]
    }
  ],
  child: [
    {
      **id: 1,**
      name: 'Jimmy Yukka',
    },
    {
      **id: 2,**
      name: 'Up North',
    },
    {
      id: 3,
      url: 'jimmyyukka.com',
      name: 'INXS',
      CreatedByUserId: 1
    }
  ],

目前我已经实现的函数代码:

  currentChildrenIds(ParentId, parentData, childData) {
    const singleParentRecord = parentData.filter(function(parent) {
      return parent.id === ParentId;
    });
    const parentsChildIds = singleParentRecord[0].books;


    const childRecords = childData.filter(function(child) {
      return child.id === parentsChildIds
    });


    return childRecords
  }

注意事项 这一点是不对的地方

const childRecords = childData.filter(function(child) {
      return child.id === parentsChildIds

这里的这一点也有点垃圾(硬编码 [0])但不是我不确定我应该如何正确编码

 const parentsChildIds = singleParentRecord[0].books;

【问题讨论】:

    标签: javascript arrays object filter


    【解决方案1】:

    这里,

    const childRecords = childData.filter(function(child) {
          return child.id === parentsChildIds
    

    parentsChildIds 是对数组的引用:您不想测试 id 是否 === 对引用,

    您必须明确并检查 id 是否包含在数组中:

    const childRecords = childData.filter(function(child) {
              return parentsChildIds.includes(child.id)
    

    关于感觉很奇怪的singleParentRecord[0], 因为您知道方法过滤器将始终返回大小为 1 或 0 的数组, 你可以使用方法find代替filter


    也在函数式编程中(数组函数,如过滤器、映射、查找...) 我建议你阅读一下箭头函数语法,因为:

    1. syntex 更加密集,让您的大脑更容易理解何时链接多个函数
    2. 如果您想使用在函数外部定义的变量,则只能在箭头函数内部使用

    带有箭头功能的代码:

    const childRecords = childData.filter((child) => {
              return child.id === parentsChildIds
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      const Parent = [
        {
          id: 1,
          name: 'Melbourne Bands',
          child: [1, 2]
        },
        {
          id: 2,
          name: 'Sydney Bands',
          child: [3]
        }
      ];
      
      const children = [
        {
          id: 1,
          name: 'Jimmy Yukka',
        },
        {
          id: 2,
          name: 'Up North',
        },
        {
          id: 3,
          url: 'jimmyyukka.com',
          name: 'INXS',
          CreatedByUserId: 1
        }
      ];
      
      // We create a new array with Array.map
      const result = Parent.map(parent => ({
        // Spread properties of the parent
        ...parent,
        // Override the child property and filter the children array with the `includes` method
        child: children.filter(child => parent.child.includes(child.id)),
      }))
      
      console.log(result);

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 1970-01-01
        • 2019-05-22
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 2023-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多