Angular 如何从多个嵌套对象中获取唯一密钥
我需要从嵌套的多个对象中提取特定的唯一键 这是我的故事:Angular 如何从多个嵌套对象中获取唯一密钥,angular,typescript,Angular,Typescript,我需要从嵌套的多个对象中提取特定的唯一键 这是我的故事: { "instance": { "$class": "org.accordproject.acceptanceofdelivery.AcceptanceOfDeliveryClause", "deliverable": { "type": "String" }, "businessDays": { "type": "Long" }, "attachment": {
{
"instance": {
"$class": "org.accordproject.acceptanceofdelivery.AcceptanceOfDeliveryClause",
"deliverable": {
"type": "String"
},
"businessDays": {
"type": "Long"
},
"attachment": {
"type": "String"
},
"clauseId": {
"type": "String"
}
},
"parties": {
"shipper": {
"$class": "org.accordproject.organization.Organization",
"identifier": {
"type": "String"
},
"name": {
"type": "String",
"optional": true
},
"description": {
"type": "String",
"optional": true
},
"duns": {
"type": "String",
"optional": true
},
"place": {
"address": {},
"geo": {
"address": {}
}
}
},
"receiver": {
"$class": "org.accordproject.organization.Organization",
"identifier": {
"type": "String"
},
"name": {
"type": "String",
"optional": true
},
"description": {
"type": "String",
"optional": true
},
"duns": {
"type": "String",
"optional": true
},
"place": {
"address": {},
"geo": {
"address": {}
}
}
}
},
"requestTypes": [
{
"$class": "org.accordproject.acceptanceofdelivery.InspectDeliverable",
"deliverableReceivedAt": {
"type": "DateTime"
},
"inspectionPassed": {
"type": "Boolean"
},
"transactionId": {
"type": "String"
},
"timestamp": {
"type": "DateTime"
}
}
],
"responseTypes": [
{
"$class": "org.accordproject.acceptanceofdelivery.InspectionResponse",
"status": {
"$class": "org.accordproject.acceptanceofdelivery.InspectionStatus",
"enum": [
"PASSED_TESTING",
"FAILED_TESTING",
"OUTSIDE_INSPECTION_PERIOD"
]
},
"shipper": {
"$class": "org.accordproject.organization.Organization",
"identifier": {
"type": "String"
},
"name": {
"type": "String",
"optional": true
},
"description": {
"type": "String",
"optional": true
},
"duns": {
"type": "String",
"optional": true
},
"place": {
"$class": "org.accordproject.geo.Place",
"address": {
"$class": "org.accordproject.address.PostalAddress",
"streetAddress": {
"type": "String",
"optional": true
},
"postalCode": {
"type": "String",
"optional": true
},
"postOfficeBoxNumber": {
"type": "String",
"optional": true
},
"addressRegion": {
"type": "String",
"optional": true
},
"addressLocality": {
"type": "String",
"optional": true
},
"addressCountry": {
"type": "String",
"optional": true
}
},
"branchCode": {
"type": "String",
"optional": true
},
"faxNumber": {
"type": "String",
"optional": true
},
"geo": {
"$class": "org.accordproject.geo.GeoCoordinates",
"address": {
"$class": "org.accordproject.address.PostalAddress",
"streetAddress": {
"type": "String",
"optional": true
},
"postalCode": {
"type": "String",
"optional": true
},
"postOfficeBoxNumber": {
"type": "String",
"optional": true
},
"addressRegion": {
"type": "String",
"optional": true
},
"addressLocality": {
"type": "String",
"optional": true
},
"addressCountry": {
"type": "String",
"optional": true
}
},
"addressCountry": {
"type": "String",
"optional": true
},
"elevation": {
"type": "Double",
"optional": true
},
"latitude": {
"type": "Double",
"optional": true
},
"longitude": {
"type": "Double",
"optional": true
}
},
"globalLocationNumber": {
"type": "String",
"optional": true
},
"mapUrl": {
"type": "String",
"optional": true
},
"isicsV4": {
"type": "String",
"optional": true
},
"telephone": {
"type": "String",
"optional": true
}
}
},
"receiver": {
"$class": "org.accordproject.organization.Organization",
"identifier": {
"type": "String"
},
"name": {
"type": "String",
"optional": true
},
"description": {
"type": "String",
"optional": true
},
"duns": {
"type": "String",
"optional": true
},
"place": {
"$class": "org.accordproject.geo.Place",
"address": {
"$class": "org.accordproject.address.PostalAddress",
"streetAddress": {
"type": "String",
"optional": true
},
"postalCode": {
"type": "String",
"optional": true
},
"postOfficeBoxNumber": {
"type": "String",
"optional": true
},
"addressRegion": {
"type": "String",
"optional": true
},
"addressLocality": {
"type": "String",
"optional": true
},
"addressCountry": {
"type": "String",
"optional": true
}
},
"branchCode": {
"type": "String",
"optional": true
},
"faxNumber": {
"type": "String",
"optional": true
},
"geo": {
"$class": "org.accordproject.geo.GeoCoordinates",
"address": {
"$class": "org.accordproject.address.PostalAddress",
"streetAddress": {
"type": "String",
"optional": true
},
"postalCode": {
"type": "String",
"optional": true
},
"postOfficeBoxNumber": {
"type": "String",
"optional": true
},
"addressRegion": {
"type": "String",
"optional": true
},
"addressLocality": {
"type": "String",
"optional": true
},
"addressCountry": {
"type": "String",
"optional": true
}
},
"addressCountry": {
"type": "String",
"optional": true
},
"elevation": {
"type": "Double",
"optional": true
},
"latitude": {
"type": "Double",
"optional": true
},
"longitude": {
"type": "Double",
"optional": true
}
},
"globalLocationNumber": {
"type": "String",
"optional": true
},
"mapUrl": {
"type": "String",
"optional": true
},
"isicsV4": {
"type": "String",
"optional": true
},
"telephone": {
"type": "String",
"optional": true
}
}
},
"transactionId": {
"type": "String"
},
"timestamp": {
"type": "DateTime"
}
}
],
"stateTypes": [
{
"$class": "org.accordproject.cicero.contract.AccordContractState",
"stateId": {
"type": "String"
}
}
],
"eventTypes": [
{
"$class": "org.accordproject.base.Event",
"eventId": {
"type": "String"
},
"timestamp": {
"type": "DateTime"
}
}
]
}
我试图在这个庞大的对象列表中一步一步地迭代,但当我试图从中获取“type”键时,总是失败
这是我的方法,但我认为我所做的部分是错误的,有更好的方法来实现这一目标
Object.keys(this.model).forEach(data => {
Object.values(this.model[data]).forEach(test => {
console.log(test);
});
我最好的结果是:
我需要像提取唯一键一样提取“type”键,这样就不会重复键值(我想要的是一次“String”,一次“DateTime”等等)。我建议分几个步骤进行
getUniqueTypeValues(flattenedObj) {
const uniqueValues = [];
Object.keys(flattenedObj).forEach(element => {
if (typeof(element) !== 'string' || !element.endsWith('type')) {
return;
}
const val = flattenedObj[element];
if (uniqueValues.find(alreadyAddedValue => val === alreadyAddedValue) === undefined) {
uniqueValues.push(val);
}
return uniqueValues;
});
}
完整解决方案
flattenObject(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object' && ob[i] !== null) {
var flatObject = flattenObject(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
}
const yourObject = {}; // Get your input
const flattenedObj = flattenObject(yourObject);
return getUniqueTypeValues(flattenedObj);
您可以在此处看到完整的解决方案:
希望有帮助