从另一个分层对象创建分层对象

时间:2019-11-14 14:47:00

标签: javascript object

我需要从另一个对象创建一个新对象。例如。 这是一个旧对象:

const objectOld = { 
  level1: { 
    level2: { 
      level3: { 
        code: 123, 
        error: 'Message' 
      }  
    } 
  } 
}

这是我根据旧对象创建的新对象。

const objectNew = { 
  level1: { 
    level2: { 
      level3: { 
        error: {
           code: 123, 
           key: 'Message' 
        }
      }  
    } 
  } 
}

深度当然会有所不同。

有人可以帮我吗?

4 个答案:

答案 0 :(得分:0)

您是否只想复制该对象?对于深度复制,您可以使用

const newObj = JSON.parse(JSON.stringify(oldObj))

答案 1 :(得分:0)

使用lodash JavaScript库进行深度复制。


const objectOld = { 
  level1: { 
    level2: { 
      level3: { 
        code: 123, 
        error: 'Message' 
      }  
    } 
  } 
}

var objectNew = _.cloneDeep(objectOld);

console.log(objectNew === objectOld)

答案 2 :(得分:0)

您可以通过撤消,Array#reduce,Object#entries和一些破坏来实现此目的。

这将深度复制您的对象,并在检测到具有{ error: { code: string, key: string } }code属性的对象时创建message

如果对象不具有这些属性,它将自行调用并继续。

如果value不是对象,请将该值保持原样。

const objectOld = { 
  level1: { 
    level2: { 
      level3: { 
        code: 123, 
        error: 'Message' 
      }  
    } 
  } 
};


function transform(obj){
  return Object
  .entries(obj)
  .reduce((acc, [key, value])=>{
    if(typeof value === 'object'){
      if("code" in value && "error" in value){
        acc[key] = {
          error: {
            key: value.error,
            code: value.code
          }
        }
      } else {
        acc[key] = transform(value)
      }
    } else {
      acc[key] = value;
    }
    return acc;
  }, {});
}

const objectNew = transform(objectOld);

console.log(
  objectNew
);

答案 3 :(得分:0)

您可以使用通用实用程序,该通用实用程序为对象中的每个键-值对调用转换函数并递归应用:

       <ul data-rules="{&quot;Pepsi_0&quot;:[&quot;3&quot;],&quot;Pepsi Twist_1&quot;:[&quot;3&quot;],&quot;Mirinda_2&quot;:[&quot;3&quot;],&quot;7 up_3&quot;:[&quot;3&quot;],&quot;Mountain Dew_4&quot;:[&quot;3&quot;]}" data-original-rules="{&quot;Pepsi_0&quot;:[&quot;3&quot;],&quot;Pepsi Twist_1&quot;:[&quot;3&quot;],&quot;Mirinda_2&quot;:[&quot;3&quot;],&quot;7 up_3&quot;:[&quot;3&quot;],&quot;Mountain Dew_4&quot;:[&quot;3&quot;]}" data-rulestype="{&quot;Pepsi_0&quot;:[&quot;&quot;],&quot;Pepsi Twist_1&quot;:[&quot;&quot;],&quot;Mirinda_2&quot;:[&quot;&quot;],&quot;7 up_3&quot;:[&quot;&quot;],&quot;Mountain Dew_4&quot;:[&quot;&quot;]}" data-tm-validation="[]" class="tmcp-ul-wrap tmcp-elements tm-extra-product-options-checkbox tm-element-ul-checkbox element_1">
                <li class="tmcp-field-wrap tmhexcolorimage-li-nowh tm-per-row">
                            <label for="tmcp_choice_1_0_2">    <input class="tmcp-field tmhexcolor_1_0_2 tm-epo-field tmcp-checkbox tcenabled" name="tmcp_checkbox_1_0" data-limit="1" data-exactlimit="" data-minimumlimit="" data-image="" data-imagec="" data-imagep="" data-imagel="" data-image-variations="[]" data-price="" data-rules="[&quot;3&quot;]" data-original-rules="[&quot;3&quot;]" data-rulestype="[&quot;&quot;]" value="Pepsi_0" id="tmcp_choice_1_0_2" tabindex="2" type="checkbox">
                        <span for="tmcp_choice_1_0_2"></span><span class="tc-label tm-label">Pepsi</span></label>	<span class="price tc-price  hidden">
                        <span class="amount">3.00lei</span>
                    </span>		
                </li>
                
                <li class="tmcp-field-wrap tmhexcolorimage-li-nowh tm-per-row">
                            <label for="tmcp_choice_1_1_3">    <input class="tmcp-field tmhexcolor_1_1_3 tm-epo-field tmcp-checkbox tcenabled" name="tmcp_checkbox_1_1" data-limit="1" data-exactlimit="" data-minimumlimit="" data-image="" data-imagec="" data-imagep="" data-imagel="" data-image-variations="[]" data-price="" data-rules="[&quot;3&quot;]" data-original-rules="[&quot;3&quot;]" data-rulestype="[&quot;&quot;]" value="Pepsi Twist_1" id="tmcp_choice_1_1_3" tabindex="3" type="checkbox">
                        <span for="tmcp_choice_1_1_3"></span><span class="tc-label tm-label">Pepsi Twist</span></label>	<span class="price tc-price  hidden">
                        <span class="amount">3.00lei</span>
                    </span>		
                </li>
                    
                <li class="tmcp-field-wrap tmhexcolorimage-li-nowh tm-per-row">
                            <label for="tmcp_choice_1_2_4">    <input class="tmcp-field tmhexcolor_1_2_4 tm-epo-field tmcp-checkbox tcenabled" name="tmcp_checkbox_1_2" data-limit="1" data-exactlimit="" data-minimumlimit="" data-image="" data-imagec="" data-imagep="" data-imagel="" data-image-variations="[]" data-price="" data-rules="[&quot;3&quot;]" data-original-rules="[&quot;3&quot;]" data-rulestype="[&quot;&quot;]" value="Mirinda_2" id="tmcp_choice_1_2_4" tabindex="4" type="checkbox">
                        <span for="tmcp_choice_1_2_4"></span><span class="tc-label tm-label">Mirinda</span></label>	<span class="price tc-price  hidden">
                        <span class="amount">3.00lei</span>
                    </span>		
                </li>
                    
                <li class="tmcp-field-wrap tmhexcolorimage-li-nowh tm-per-row">
                            <label for="tmcp_choice_1_3_5">    <input class="tmcp-field tmhexcolor_1_3_5 tm-epo-field tmcp-checkbox tcenabled" name="tmcp_checkbox_1_3" data-limit="1" data-exactlimit="" data-minimumlimit="" data-image="" data-imagec="" data-imagep="" data-imagel="" data-image-variations="[]" data-price="" data-rules="[&quot;3&quot;]" data-original-rules="[&quot;3&quot;]" data-rulestype="[&quot;&quot;]" value="7 up_3" id="tmcp_choice_1_3_5" tabindex="5" type="checkbox">
                        <span for="tmcp_choice_1_3_5"></span><span class="tc-label tm-label">7 up</span></label>	<span class="price tc-price  hidden">
                        <span class="amount">3.00lei</span>
                    </span>		
                </li>
                    
                <li class="tmcp-field-wrap tmhexcolorimage-li-nowh tm-per-row">
                            <label for="tmcp_choice_1_4_6">    <input class="tmcp-field tmhexcolor_1_4_6 tm-epo-field tmcp-checkbox tcenabled" name="tmcp_checkbox_1_4" data-limit="1" data-exactlimit="" data-minimumlimit="" data-image="" data-imagec="" data-imagep="" data-imagel="" data-image-variations="[]" data-price="" data-rules="[&quot;3&quot;]" data-original-rules="[&quot;3&quot;]" data-rulestype="[&quot;&quot;]" value="Mountain Dew_4" id="tmcp_choice_1_4_6" tabindex="6" type="checkbox">
                        <span for="tmcp_choice_1_4_6"></span><span class="tc-label tm-label">Mountain Dew</span></label>	<span class="price tc-price  hidden">
                        <span class="amount">3.00lei</span>
                    </span>		
                </li>
            </ul>