小编Bar*_*art的帖子

从简单数组创建嵌套对象

我正在尝试从一个简单的数组中创建一个深层嵌套的 JS 对象。棘手的部分是数组中的下一项应始终添加到前一项。

假设我的数组如下所示:

const filters = [
    [
        {brand: {eq: 'BMW'}},
        {brand: {eq: 'AUDI'}}
    ],
    [
        {year: {eq: '2019'}},
        {year: {eq: '2020'}}
    ],
    [
        {country: {eq: 'CH'}},
        {country: {eq: 'DE'}}
    ]
]
Run Code Online (Sandbox Code Playgroud)

如何获得具有该结构的对象?

    query: {
      and: {
        or: [
          { brand: { eq: 'BMW' } },
          { brand: { eq: 'AUDI' } }
        ],
        and: {
          or: [
            { year: { eq: '2019' } },
            { year: { eq: '2020' } }
          ],
          and: {
            or: [
              { country: …
Run Code Online (Sandbox Code Playgroud)

javascript arrays

1
推荐指数
1
解决办法
57
查看次数

标签 统计

arrays ×1

javascript ×1