如何在子元素上执行操作并退还父母?

发布于 2025-02-08 12:42:34 字数 931 浏览 3 评论 0原文

我有这样的数组,

const array = [
  { 
    name: 'Parent Brand 1', 
    childBrands: [
      { name: 'Child Brand 1', status: 'active' },
      { name: 'Child Brand 2', status: 'discontinued' },
    ] 
  }
, { 
    name: 'Parent Brand 2',
    childBrands: [
      { name: 'Child Brand 1', status: 'discontinued' },
      { name: 'Child Brand 2', status: 'active' },
    ] 
  }
];

该如何制作它,以便它通过状态过滤儿童品牌并返回父对象?通过“活动”状态过滤后,它应该返回这样的东西,

const array = [
  { 
    name: 'Parent Brand 1', 
    childBrands: [
      { name: 'Child Brand 1', status: 'active' },
    ] 
  }
, { 
    name: 'Parent Brand 2',
    childBrands: [
      { name: 'Child Brand 2', status: 'active' },
    ] 
  }
];

使用flatmapfilter仅在需要父对象(包括子元素)的父对象时返回子元素

{ "name": "Child Brand 1","status": "active" }

{ "name": "Child Brand 2","status": "active" }

I have an array like this

const array = [
  { 
    name: 'Parent Brand 1', 
    childBrands: [
      { name: 'Child Brand 1', status: 'active' },
      { name: 'Child Brand 2', status: 'discontinued' },
    ] 
  }
, { 
    name: 'Parent Brand 2',
    childBrands: [
      { name: 'Child Brand 1', status: 'discontinued' },
      { name: 'Child Brand 2', status: 'active' },
    ] 
  }
];

How do I make it so that it filters the child brands by status and returns the parent object? After filtering by 'active' status it should return something like this,

const array = [
  { 
    name: 'Parent Brand 1', 
    childBrands: [
      { name: 'Child Brand 1', status: 'active' },
    ] 
  }
, { 
    name: 'Parent Brand 2',
    childBrands: [
      { name: 'Child Brand 2', status: 'active' },
    ] 
  }
];

Using a flatMap and filter only returns the child elements when i need the parent object including the child element

{ "name": "Child Brand 1","status": "active" }

{ "name": "Child Brand 2","status": "active" }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕梦 2025-02-15 12:42:34

由于您没有发布任何代码,因此在这种情况下,很难知道如何使用flatmap()。您可以简单地map()在数组上并过滤每个嵌套数组属性。

const array = [{ name: 'Parent Brand 1', childBrands: [{ name: 'Child Brand 1', status: 'active' }, { name: 'Child Brand 2', status: 'discontinued' },] }, { name: 'Parent Brand 2', childBrands: [{ name: 'Child Brand 1', status: 'discontinued' }, { name: 'Child Brand 2', status: 'active' },] }];

const filtered = array.map(parent => ({
  ...parent,
  childBrands: parent.childBrands.filter(child => child.status === 'active')
}));

console.log(filtered)

Since you haven't posted any code it's hard to know how you would be using flatMap() in this case. You can simply map() over the array and filter each nested array property.

const array = [{ name: 'Parent Brand 1', childBrands: [{ name: 'Child Brand 1', status: 'active' }, { name: 'Child Brand 2', status: 'discontinued' },] }, { name: 'Parent Brand 2', childBrands: [{ name: 'Child Brand 1', status: 'discontinued' }, { name: 'Child Brand 2', status: 'active' },] }];

const filtered = array.map(parent => ({
  ...parent,
  childBrands: parent.childBrands.filter(child => child.status === 'active')
}));

console.log(filtered)

一场春暖 2025-02-15 12:42:34

您在主要数组上使用数组#映射,而Array#filter在次要数组上如下:

const array = [ { name: 'Parent Brand 1', childBrands: [ { name: 'Child Brand 1', status: 'active' }, { name: 'Child Brand 2', status: 'discontinued' }, ] } , { name: 'Parent Brand 2', childBrands: [ { name: 'Child Brand 1', status: 'discontinued' }, { name: 'Child Brand 2', status: 'active' }, ] } ],

      output = array.map(
          ({childBrands,...rest}) =>
          ({
              ...rest,
              childBrands:childBrands.filter(
                  ({status}) => status === 'active'
              )
          })
      );
      
console.log( output );

You use Array#map on the major array and Array#filter on the minor arrays as follows:

const array = [ { name: 'Parent Brand 1', childBrands: [ { name: 'Child Brand 1', status: 'active' }, { name: 'Child Brand 2', status: 'discontinued' }, ] } , { name: 'Parent Brand 2', childBrands: [ { name: 'Child Brand 1', status: 'discontinued' }, { name: 'Child Brand 2', status: 'active' }, ] } ],

      output = array.map(
          ({childBrands,...rest}) =>
          ({
              ...rest,
              childBrands:childBrands.filter(
                  ({status}) => status === 'active'
              )
          })
      );
      
console.log( output );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文