我需要从嵌套的多个对象中提取特定的唯一键。
这是我的说法:
{
"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"
}
}
]
}
我试图在庞大的对象列表中逐步进行迭代,但是当我尝试从中获取“类型”键时,我总是失败。
这是我的方法,但我认为我正在做的事情中有一部分是错误的,并且有更好的方法可以实现这一目标
Object.keys(this.model).forEach(data => {
Object.values(this.model[data]).forEach(test => {
console.log(test);
});
我最好的结果是这样
我需要像唯一键一样提取“类型”键,因此我不会重复使用键值(我打算一次是“字符串”,一次是“ DateTime”,依此类推)。
答案 0 :(得分:1)
我建议分几个步骤进行。
扁平对象(可能需要一些修改)
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;
}
将键置于上方并添加到Array
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;
});
}
完整解决方案
const yourObject = {}; // Get your input
const flattenedObj = flattenObject(yourObject);
return getUniqueTypeValues(flattenedObj);
您可以在此处看到完整的解决方案:https://jsfiddle.net/zrhyk3ap/
希望有帮助!