我有以下内容:
{
"aGame":"STAR",
"eleSort":[
{
"groupId":"mix",
"groupName":"mixed kind"
},
{
"groupId":"unmixed",
"groupName":"unmixed kind"
}
],
"sortsKind":[
{
"sortsKindId":"post_zipcodeext",
"attrGroups":[
"mix",
"unmixed"
],
"row":11,
"column":1,
"regex":"[0-9]{3}-[0-9]{3}-[0-9]{4}"
},
{
"sortsKindId":"post_orgState_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":4,
"column":1,
"regex":null,
"selectValues":[
{
"value":"Ohio",
"key":"OH"
},
{
"value":"Oklahoma",
"key":"OK"
},
{
"value":"Oregon",
"key":"OR"
},
{
"value":"Pennsylvania",
"key":"PA"
},
{
"value":"Rhode Island",
"key":"RI"
},
{
"value":"South Carolina",
"key":"SC"
},
{
"value":"South Dakota",
"key":"SD"
},
{
"value":"Tennessee",
"key":"TN"
},
{
"value":"Texas",
"key":"TX"
},
{
"value":"Utah",
"key":"UT"
},
{
"value":"Vermont",
"key":"VT"
},
{
"value":"Virginia ",
"key":"VA"
},
{
"value":"Washington",
"key":"WA"
},
{
"value":"West Virginia",
"key":"WV"
},
{
"value":"Wisconsin",
"key":"WI"
},
{
"value":"Wyoming",
"key":"WY"
}
]
},
{
"sortsKindId":"post_orgTINSearchButton",
"attrGroups":[
"mix"
],
"row":5,
"column":1,
"regex":null,
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_tinssn_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":1,
"column":1,
"regex":"(([EHRSXehsrx9]{1}[0-9]{4}),[ ]*)*([EHRSXehsrx9]{1}[0-9]{4})",
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_lbn_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":2,
"column":1,
"regex":"(([EHRSXehsrx9]{1}[0-9]{4}),[ ]*)*([EHRSXehsrx9]{1}[0-9]{4})",
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_tin",
"attrGroups":[
"mix",
"unmixed"
],
"row":7,
"column":1,
"regex":"(([EHRSXehsrx9]{1}[0-9]{4}),[ ]*)*([EHRSXehsrx9]{1}[0-9]"
},
{
"sortsKindId":"post_lbn",
"attrGroups":[
"mix",
"unmixed"
],
"row":8,
"column":1
},
{
"sortsKindId":"post_certNumber",
"attrGroups":[
"unmixed"
]
},
{
"sortsKindId":"post_phonenum",
"attrGroups":[
"unmixed"
],
"row":10,
"column":1,
"regex":"[0-9]{3}-[0-9]{3}-[0-9]{4}",
"validationMsg":"Enter information in valid fomat"
},
{
"sortsKindId":"post_ext",
"attrGroups":[
"unmixed"
],
"row":13,
"column":1,
"regex":"Numeric *************"
},
{
"sortsKindId":"post_compfax",
"attrGroups":[
"unmixed"
],
"row":15,
"column":1,
"regex":"XXX-XXX-XXXX Numeric Dash (-) ************",
"validationMsg":"1. <Field Name>: Enter the information in this field in the correct format."
},
{
"sortsKindId":"post_faxnumext",
"attrGroups":[
"unmixed"
],
"row":16,
"column":1,
"regex":"[0-9]{0,5}",
"validationMsg":"1. <Field Name>: Your entry contains an invalid character. Please re-type your entry. 2. <Field Name>: Enter the information in this field in the correct format."
},
{
"sortsKindId":"post_add1",
"attrGroups":[
"unmixed"
],
"row":14,
"column":1,
"regex":"************* ASCII except brackets < or > or ) or ( ***********",
"validationMsg":"1. <Field Name>: The required field is missing information. 2. <Field Name>: Your entry contains an invalid character. Please re-type your entry."
},
{
"sortsKindId":"post_add2",
"attrGroups":[
"unmixed"
],
"row":17,
"column":1,
"regex":"************* ASCII except brackets < or > or ) or ( *************",
"validationMsg":"1. <Field Name>: Your entry contains an invalid character. Please re-type your entry."
},
{
"sortsKindId":"post_orgCity_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":3,
"column":1,
"regex":"[A-Za-z- '.]+",
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_zipcode",
"attrGroups":[
"unmixed"
],
"row":12,
"column":1,
"regex":"[0-9]{5}",
"validationMsg":"1. <Field Name>: The required field is missing information. 2. <Field Name>: Enter the information in this field in the correct format."
},
{
"sortsKindId":"post_country",
"attrGroups":[
"unmixed"
],
"row":18,
"column":1,
"regex":"Default United States *************",
"validationMsg":"1. <Field Name>: Your entry contains an invalid character. Please re-type your entry."
}
]
}
如果您阅读它,将会看到sortsKind是一个对象数组。 在此数组内的每个对象中,您将找到一个名为attrGroups的属性,该属性是一个数组。一些对象在此数组中具有两个元素。但是我希望每个对象在attrGroups数组中只有一个元素。意思是,如果您在attrGroups数组中看到两个元素,则我希望将整个对象克隆两次,并且其中仅包含一个元素。第一个对象具有混合的attrGroup,第二个对象具有未混合的attrGroup。我想通过打字稿功能来实现这一点。
答案 0 :(得分:0)
这是您想要的吗?
const a = {
"aGame":"STAR",
"eleSort":[
{
"groupId":"mix",
"groupName":"mixed kind"
},
{
"groupId":"unmixed",
"groupName":"unmixed kind"
}
],
"sortsKind":[
{
"sortsKindId":"post_zipcodeext",
"attrGroups":[
"mix",
"unmixed"
],
"row":11,
"column":1,
"regex":"[0-9]{3}-[0-9]{3}-[0-9]{4}"
},
{
"sortsKindId":"post_orgState_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":4,
"column":1,
"regex":null,
"selectValues":[
{
"value":"Ohio",
"key":"OH"
},
{
"value":"Oklahoma",
"key":"OK"
},
{
"value":"Oregon",
"key":"OR"
},
{
"value":"Pennsylvania",
"key":"PA"
},
{
"value":"Rhode Island",
"key":"RI"
},
{
"value":"South Carolina",
"key":"SC"
},
{
"value":"South Dakota",
"key":"SD"
},
{
"value":"Tennessee",
"key":"TN"
},
{
"value":"Texas",
"key":"TX"
},
{
"value":"Utah",
"key":"UT"
},
{
"value":"Vermont",
"key":"VT"
},
{
"value":"Virginia ",
"key":"VA"
},
{
"value":"Washington",
"key":"WA"
},
{
"value":"West Virginia",
"key":"WV"
},
{
"value":"Wisconsin",
"key":"WI"
},
{
"value":"Wyoming",
"key":"WY"
}
]
},
{
"sortsKindId":"post_orgTINSearchButton",
"attrGroups":[
"mix"
],
"row":5,
"column":1,
"regex":null,
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_tinssn_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":1,
"column":1,
"regex":"(([EHRSXehsrx9]{1}[0-9]{4}),[ ]*)*([EHRSXehsrx9]{1}[0-9]{4})",
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_lbn_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":2,
"column":1,
"regex":"(([EHRSXehsrx9]{1}[0-9]{4}),[ ]*)*([EHRSXehsrx9]{1}[0-9]{4})",
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_tin",
"attrGroups":[
"mix",
"unmixed"
],
"row":7,
"column":1,
"regex":"(([EHRSXehsrx9]{1}[0-9]{4}),[ ]*)*([EHRSXehsrx9]{1}[0-9]"
},
{
"sortsKindId":"post_lbn",
"attrGroups":[
"mix",
"unmixed"
],
"row":8,
"column":1
},
{
"sortsKindId":"post_certNumber",
"attrGroups":[
"unmixed"
]
},
{
"sortsKindId":"post_phonenum",
"attrGroups":[
"unmixed"
],
"row":10,
"column":1,
"regex":"[0-9]{3}-[0-9]{3}-[0-9]{4}",
"validationMsg":"Enter information in valid fomat"
},
{
"sortsKindId":"post_ext",
"attrGroups":[
"unmixed"
],
"row":13,
"column":1,
"regex":"Numeric *************"
},
{
"sortsKindId":"post_compfax",
"attrGroups":[
"unmixed"
],
"row":15,
"column":1,
"regex":"XXX-XXX-XXXX Numeric Dash (-) ************",
"validationMsg":"1. <Field Name>: Enter the information in this field in the correct format."
},
{
"sortsKindId":"post_faxnumext",
"attrGroups":[
"unmixed"
],
"row":16,
"column":1,
"regex":"[0-9]{0,5}",
"validationMsg":"1. <Field Name>: Your entry contains an invalid character. Please re-type your entry. 2. <Field Name>: Enter the information in this field in the correct format."
},
{
"sortsKindId":"post_add1",
"attrGroups":[
"unmixed"
],
"row":14,
"column":1,
"regex":"************* ASCII except brackets < or > or ) or ( ***********",
"validationMsg":"1. <Field Name>: The required field is missing information. 2. <Field Name>: Your entry contains an invalid character. Please re-type your entry."
},
{
"sortsKindId":"post_add2",
"attrGroups":[
"unmixed"
],
"row":17,
"column":1,
"regex":"************* ASCII except brackets < or > or ) or ( *************",
"validationMsg":"1. <Field Name>: Your entry contains an invalid character. Please re-type your entry."
},
{
"sortsKindId":"post_orgCity_si",
"attrGroups":[
"mix",
"unmixed"
],
"row":3,
"column":1,
"regex":"[A-Za-z- '.]+",
"validationMsg":"1. Your search returned no results. Please change your search criteria and try again."
},
{
"sortsKindId":"post_zipcode",
"attrGroups":[
"unmixed"
],
"row":12,
"column":1,
"regex":"[0-9]{5}",
"validationMsg":"1. <Field Name>: The required field is missing information. 2. <Field Name>: Enter the information in this field in the correct format."
},
{
"sortsKindId":"post_country",
"attrGroups":[
"unmixed"
],
"row":18,
"column":1,
"regex":"Default United States *************",
"validationMsg":"1. <Field Name>: Your entry contains an invalid character. Please re-type your entry."
}
]
}
const b = a.sortsKind.reduce((sum, val) => {
val.attrGroups.forEach(element => {
const cloned = JSON.parse(JSON.stringify(val)); // clone object
cloned.attrGroups = [element];
sum.push(cloned);
});
return sum;
}, []);
console.log(b);