>source

i는 키 이름으로 특정 객체를 가져 오려는 중첩 된 배열 개체가있는 JSON 배열을 피했습니다. 나는 많이 시도했지만 나는 일반적인 해결책이 필요합니다.

let data= {"role":"http://www.icai.org/xbrl/taxonomy/role/in-ca/DisclosureOfGeneralInformationAboutCompany","facts":{"concept":"in-ca:DisclosureOfGeneralInformationAboutCompanyAbstract","$t":"","facts":[{"concept":"in-ca:DisclosureOfCompanyInformationAbstract","$t":"","facts":[{"concept":"in-ca:NameOfCompany","d2016":"LAKSHMI ENERGY AND FOODS LIMITED"},{"concept":"in-ca:CorporateIdentityNumber","d2016":"L00000CH1990PLC010573"},{"concept":"in-ca:PermanentAccountNumberOfEntity","d2016":"AAACL3147J"},{"concept":"in-ca:AddressOfRegisteredOfficeOfCompany","d2016":"SCO 18  19  1ST FLOOR SECTOR 9 D, MADHYA MARH   CHANDIGARH"},{"concept":"in-ca:TypeOfIndustry","d2016":"Commercial and Industrial"},{"concept":"in-ca:RegistrationDate"},{"concept":"in-ca:CategoryOrSubcategoryOfCompany"},{"concept":"in-ca:WhetherCompanyIsListedCompany"}]},{"concept":"in-ca:DisclosureOfDocumentInformationAbstract","$t":"","facts":[{"concept":"in-ca:DateOfBoardMeetingWhenFinalAccountsWereApproved","d2016":"2016-05-09"},{"concept":"in-ca:PeriodCoveredByFinancialStatements"},{"concept":"in-ca:DateOfStartOfReportingPeriod","d2015":"2014-04-01","d2016":"2015-04-01"},{"concept":"in-ca:DateOfEndOfReportingPeriod","d2015":"2015-03-31","d2016":"2016-03-31"},{"concept":"in-ca:NatureOfReportStandaloneConsolidated","d2016":"Standalone"},{"concept":"in-ca:ContentOfReport","d2016":"Financial Statements"},{"concept":"in-ca:DescriptionOfPresentationCurrency","d2016":"INR"},{"concept":"in-ca:LevelOfRoundingUsedInFinancialStatements","d2016":"Millions"},{"concept":"in-ca:TypeOfCashFlowStatement","d2016":"Indirect Method"},{"concept":"in-ca:DisclosureWebLinkOfCompanyAtWhichAnnualReportIsPlaced"}]},{"concept":"in-ca:DisclosureOfOtherGeneralInformationAbstract","$t":"","facts":[{"concept":"in-ca:DateFromWhichRegisterOfMembersRemainedClosed"},{"concept":"in-ca:DateTillWhichRegisterOfMembersRemainedClosed"},{"concept":"in-ca:NameOfRegistrarAndTransferAgent"},{"concept":"in-ca:AddressAndContactDetailsOfRegistrarAndTransferAgent"},{"concept":"in-ca:WhetherCompanyIsMaintainingBooksOfAccountAndOtherRelevantBooksAndPapersInElectronicForm","d2016":"true"},{"concept":"in-ca:PostalAddressOfPlaceOfMaintenanceOfComputerServersStoringAccountingDataAbstract","facts":[{"concept":"in-ca:CompletePostalAddressOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"Lakshmi Energy and Foods Limited Chandigarh-Ludhiana National Highway, at Khamanon, Punjab,"},{"concept":"in-ca:NameOfCityOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"Chandigarh-Ludhiana National Highway, at Khamanon, Punjab,"},{"concept":"in-ca:NameOfStateUnionTerritoryOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"Punjab"},{"concept":"in-ca:PinCodeOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"141801"},{"concept":"in-ca:NameOfDistrictOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"Punjab"},{"concept":"in-ca:ISOCountryCodeOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"IN"},{"concept":"in-ca:NameOfCountryOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"India"},{"concept":"in-ca:PhoneWithSTDISDCodeOfPlaceOfMaintenanceOfComputerServersStoringAccountingData","d2016":"01628-661800"}]},{"concept":"in-ca:ParticularsOfServiceProviderAbstract","$t":"","facts":[{"concept":"in-ca:NameOfTheServiceProvider"},{"concept":"in-ca:InternetProtocolAddressOfServiceProvider"},{"concept":"in-ca:LocationOfTheServiceProvider"},{"concept":"in-ca:WhetherBooksOfAccountAndOtherBooksAndPapersAreMaintainedOnCloud"},{"concept":"in-ca:AddressAsProvidedByTheServiceProvider"}]}]},{"concept":"in-ca:DisclosureOfPrincipalProductOrServicesAbstract","$t":"","facts":[{"concept":"in-ca:TotalNumberOfProductOrServiceCategory"},{"concept":"in-ca:DescriptionOfPrincipalProductOrServicesCategory"},{"concept":"in-ca:DisclosureOfPrincipalProductOrServicesTable"},{"concept":"in-ca:DisclosureOfPrincipalProductOrServicesLineItems","$t":"","facts":[{"concept":"in-ca:ProductOrServiceCategoryITC4DigitCode","d2016_TypesOfPrincipalProductOrServicesAxis_Prod1":"1006"},{"concept":"in-ca:DescriptionOfProductOrServiceCategory","d2016_TypesOfPrincipalProductOrServicesAxis_Prod1":"Rice and Paddy"},{"concept":"in-ca:TurnoverOfProductOrServiceCategory","d2016_TypesOfPrincipalProductOrServicesAxis_Prod1":"8,784,320,000.00"},{"concept":"in-ca:HighestTurnoverContributingProductOrServiceITC8DigitCode","d2016_TypesOfPrincipalProductOrServicesAxis_Prod1":"10061090"},{"concept":"in-ca:DescriptionOfProductOrService","d2016_TypesOfPrincipalProductOrServicesAxis_Prod1":"Rice and Paddy"},{"concept":"in-ca:UnitOfMeasurementOfHighestContributingProductOrService","d2016_TypesOfPrincipalProductOrServicesAxis_Prod1":"kg"},{"concept":"in-ca:TurnoverOfHighestContributingProductOrService","d2016_TypesOfPrincipalProductOrServicesAxis_Prod1":"8,784,320,000.00"},{"concept":"in-ca:QuantityOfHighestContributingProductOrServiceInUom"}]}]}]}}

실제로이 객체에는 개념 키가 전달 된 키와 일치하는 경우 데이터를 가져와야하는 경우 사실 키가 있으므로 특정 객체를 반환해야합니다. 일반적인 솔루션이있는 경우 공유하십시오. 나는 한 솔루션을 만들었지 만 그것은 더러운 것이고 어떤 시점에서 깨질 수있는 더러운 것입니다.

const getObj= (data, key, subKey)=>{
let obj= {};
key= `:${key}`;
data.forEach((v)=>{
    if(v.facts){
        if(v.facts.length > 0){
            v.facts.forEach((m)=>{
                if(m.facts){
                    if(m.facts.length > 0){
                        m.facts.forEach((n)=>{
                            if(n.facts){
                                if(n.facts.length > 0){
                                    n.facts.forEach((o)=>{
                                        if(o.concept.includes(key)) {
                                            obj= o;
                                            return true;
                                        }
                                    })
                                }
                                else{
                                    if(n.concept.includes(key)) {
                                        obj= n;
                                        return true;
                                    }
                                }
                            }
                            else {
                                if(n.concept.includes(key)) {
                                    obj= n;
                                    return true;
                                }
                            }
                        })
                    }
                    else{
                        if(m.concept.includes(key)) {
                            obj= m;
                            return true;
                        }
                    }
                }
                else{
                    if(m.concept.includes(key)) {
                        obj= m;
                        return true;
                    }
                }
            })
        }
        else{
            if(v.concept.includes(key)) {
                obj= v;
                return true;
            }
        }
    }
    else{
        if(v.concept== key) {
            obj= v;
            return true;
        }
    }
});
let value= "";
if(obj[`i${subKey}`]) value= obj[`i${subKey}`];
else if(obj[`d${subKey}`]) value= obj[`d${subKey}`];
else if(obj[subKey]) value= obj[subKey];
return value;}

이걸 도와주세요

사실은 항상 배열입니까? 귀하가 요청한 것을 추가하고 그 결과를 추가하십시오.

Nina Scholz2021-06-16 07:11:05

나는 너무 많은 forech 루프가 안쪽에 중첩되어 깊이 가고 깊이 가고 있습니다. code에 먼저 많은 정리가 필요합니다. 이 code는 너무 많은 메모리를 가져 오기 때문에 지금까지 느리게해야합니다.

Always Helping2021-06-16 07:12:01

@ninascholz 사실이 중첩 된 경우에만 중첩 된 배열이없는 경우 객체 일 수 있습니다.

sainanky2021-06-16 07:16:36

Return true foreach를 종료하지 않습니다. 배열의 각 항목을 처리합니다.

adiga2021-06-16 07:18:47

또한 입력 및 예상 출력을 추가하십시오. 아마도 중첩 된 객체를 가로 지르는 간단한 재귀로 이것을 해결할 수 있습니다.

adiga2021-06-16 07:21:29
  • 이전 node.js : 프로토콜 오류 -HTML-PDF 노드 NPM 패키지
  • 다음 VSCODE 원격 컨테이너의 포워드 포트에 대해 청취 주소 0.0.0.0을 지정하십시오.