【问题标题】:Recursivly removing unecessary nesting in array of objects递归删除对象数组中不必要的嵌套
【发布时间】:2021-04-24 03:42:23
【问题描述】:

这里是对象数组示例:

const test = [
  {
    properties: {
      test1: "aa",
      test2: "bb",
      button: [
        {
          properties: {
            btnTest1: "aa",
            btnTest2: "bb"
          }
        }
      ]
    }
  },
  {
    properties: {
      test1: "aa",
      test2: "bb",
      button: [
        {
          properties: {
            btnTest1: "aa",
            btnTest2: "bb"
          }
        }
      ]
    },
  }
]

使用递归函数,我希望能够以某种方式摆脱"properties" 键,因此结果将是:

const test = [
  {
    test1: "aa",
    test2: "bb",
    button: [
      {
        btnTest1: "aa",
        btnTest2: "bb"
      }
    ]
  },
  {
     test1: "aa",
     test2: "bb",
     button: [
       {
         btnTest1: "aa",
         btnTest2: "bb"
       }
     ]
  }
]

我正在努力实现这一目标,我真的没有任何我正在处理的代码可以在这里分享。 感谢任何愿意提供帮助的人。

【问题讨论】:

  • 如果属性对象不相同,预期的结果是什么?
  • 你想要的输出有一个对象,它的属性是test1 的两倍。并且您的原始对象有一个具有两倍 properties 属性的对象...似乎您想要不同的东西...
  • 是的,对不起我的错...我更正了我想要的结果。

标签: javascript arrays object recursion ecmascript-6


【解决方案1】:

这种方法与其他答案有些不同。首先,它使要更改的属性名称(此处为 "properties")可配置。更重要的是,这并不假定 "properties"(或其他)是被修改对象的唯一属性。所以

{
  foo: 42,
  properties: {bar: 'abc'}
}

会变成

{
  foo: 42,
  bar: 'abc'
}

该功能可能对您很重要,也可能不重要,但它是一个有趣的选择。

这确实允许一些歧义。在此实现中,如果在根和内部属性中定义了名称,则后者将覆盖前者。但这很容易改变。

const flatProp = (p) => (o) => 
  Array .isArray (o)
    ? o .map (flatProp (p))
  : Object (o) === o
    ? Object .fromEntries (Object .entries (o) .flatMap (
        ([k, v]) =>  k === p 
          ? Object .entries (v) .map (([k, v]) => [k, flatProp(p)(v)]) 
          : [[k, v]]
      ))
  : o


const input = [{properties: {test1: "aa", test2: "bb", button: [{properties: {btnTest1: "aa", btnTest2: "bb"}}]}}, {properties: {test1: "aa", test2: "bb", button: [{properties: {btnTest1: "aa", btnTest2: "bb"}}]}}]

console .log (flatProp ('properties') (input))
.as-console-wrapper {max-height: 100% !important; top: 0}

当然,如果您愿意,可以部分应用这个柯里化函数:

const upgradeProperties = flatProp ('properties')

console .log (upgradeProperties (input))

如果您被困在没有Object.fromEntries 的环境中,则很容易进行 shim。

【讨论】:

    【解决方案2】:

    您可以进行各种检查并创建没有不需要的属性的新对象。

    function remove(value) {
        if (!value || typeof value !== 'object') return value;
        if (Array.isArray(value)) return value.map(remove);
        if ('properties' in value) return remove(value.properties);
        return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, remove(v)]));
    }
    
    const test = [{ properties: { test1: "aa", test2: "bb", button: [{ properties: { btnTest1: "aa", btnTest2: "bb" } }] }}, { properties: { test1: "aa", test2: "bb", button: [{ properties: { btnTest1: "aa", btnTest2: "bb" } }] } }];
    
    console.log(remove(test));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 我遇到了 Object.fromEntries 在节点版本 > 12 之前不受支持的问题。我的节点版本是 v12.16.3,有什么解决办法吗?
    • 更新的节点版本?否则,您可以减少数组:return Object.entries(value).reduce((o, [k, v]) => { o[k] = remove(v); return o; }, {});
    • 你是最棒的,工作就像一个魅力 :) 但我肯定需要更多时间来了解整个功能。
    【解决方案3】:

    如果没有properties 键,您可以使用reduce 方法和for..in 循环来遍历对象。此解决方案假定当有 properties 键时,该对象中没有其他键。

    const test = [{"properties":{"test1":"aa","test2":"bb","button":[{"properties":{"btnTest1":"aa","btnTest2":"bb"}}]}},{"properties":{"test1":"aa","test2":"bb","button":[{"properties":{"btnTest1":"aa","btnTest2":"bb"}}]}}]
    
    function f(data) {
      return data.reduce((r, e) => {
        if (e.properties) {
          r.push(...f([e.properties]))
        } else {
          const o = {}
    
          for (let p in e) {
            if (Array.isArray(e[p])) {
              o[p] = f(e[p])
            } else {
              o[p] = e[p]
            }
          }
    
          r.push(o)
        }
    
        return r
      }, [])
    }
    
    console.log(f(test))

    【讨论】:

    • 非常感谢。我会说这比我想象的要复杂......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2012-03-15
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多