如何将对象的深层属性转换为另一个属性?

5
以下是一个具有递归属性的深度嵌套对象。
如何转换以下深度嵌套对象:
const obj = {
    prop1: {
        properties: { value: {} },
    },
    prop2: {
        properties: { subProp: { properties: { value: {} } } },
    },
    prop3: {
        properties: { subProp: { properties: { subSubProp: { properties: { value: {} } } } } },
    },
};

转换为:

const obj = {
    prop1: { value: {} },
    prop2: { subProp: { value: {} } },
    prop3: { subProp: { subSubProp: { value: {} } } },
};

//if properties exists, else leave as it is (in each level)
3个回答

6
您可以构建新的对象并检查该值是否为对象,以及properties是否存在。然后,将properties或该对象用于递归调用。

const
    removeProperties = object => Object.fromEntries(Object
        .entries(object)
        .map(([key, value]) => [
            key,
            value && typeof value === 'object'
                ? removeProperties('properties' in value ? value.properties : value)
                : value
        ])
    ),
    obj = { prop1: { properties: { value: {} } }, prop2: { properties: { subProp: { properties: { value: {} } } } }, prop3: { properties: { subProp: { properties: { subSubProp: { properties: { value: {} } } } } } } };

console.log(removeProperties(obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

没有 Object.fromEntries

const
    removeProperties = object => Object
        .entries(object)
        .map(([key, value]) => [
            key,
            value && typeof value === 'object'
                ? removeProperties('properties' in value ? value.properties : value)
                : value
        ])
        .reduce((object, [key, value]) => ({ ...object, [key]: value }), {}),
    obj = { prop1: { properties: { value: {} } }, prop2: { properties: { subProp: { properties: { value: {} } } } }, prop3: { properties: { subProp: { properties: { subSubProp: { properties: { value: {} } } } } } } };

console.log(removeProperties(obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }


应该使用递归完成,即所有深层级别。 - reacg garav
目前这只进行了一层。 - reacg garav
属性“fromEntries”在类型“ObjectConstructor”上不存在 - 错误。我在TypeScript中使用它。 - reacg garav
嗨,Nina。你能否也回答这个问题吗? https://stackoverflow.com/questions/62362111/how-to-transfer-props-of-an-object-from-one-location-to-another/62362652?noredirect=1#comment110295195_62362652 - reacg garav
@reacggarav,我对这个新问题一无所知。顺便问一下,你有接受过答案吗?你也会因此获得奖励。 - Nina Scholz
问题很简单。只需在相应的占位符处将对象的props从一个级别转移到另一个级别。 - reacg garav

1
您可以递归地删除它。

 const obj = {
      prop1: {
        properties: { value: {} },
      },
      prop2: {
        properties: { subProp: { properties: { value: {} } } },
      },
      prop3: {
        properties: { subProp: { properties: { subSubProp: { properties: { value: {} } } } } },
      },
    };

function converter(obj){
  if(typeof obj !== "object") return

  Object.entries(obj).forEach(([key, value]) => {
    if(value.properties){
      obj[key] = value.properties
      delete value.properties
    }
    if(typeof obj[key] === "object"){
      converter(obj[key])
    }
  })
}

converter(obj)
console.log(obj)


你能否也回答这个问题呢? https://stackoverflow.com/questions/62362111/how-to-transfer-props-of-an-object-from-one-location-to-another/62362652?noredirect=1#comment110295195_62362652 - reacg garav

1
尝试这个,这个递归方法也适用于嵌套删除数据。
const obj = {
  prop1: {
    properties: { value: {} },
  },
  prop2: {
    properties: { subProp: { properties: { value: {} } } },
  },
  prop3: {
    properties: { subProp: { properties: { subSubProp: { properties: { value: {} } } } } },
  },
};

function propsRemover(data) {
  return Object.fromEntries(Object.entries(data).map(([key, { properties }]) => [key, propsRemover({ ...properties })]));
}

console.log(JSON.stringify(propsRemover(obj)));

你能否也回答一下这个问题呢? https://stackoverflow.com/questions/62362111/how-to-transfer-props-of-an-object-from-one-location-to-another/62362652?noredirect=1#comment110295195_62362652 - reacg garav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接