Searches in Objects without knowing the attribute name

Asked

Viewed 65 times

-4

I have a rather large object on which I need to search it. I have several objects inside and several arrays.For this reason it would not be feasible to give one console.log in each attribute until you find what you wanted. It would be quite laborious,: Dentrodeobject object searches for "string" the code I created was this one

var xml         = require("fs");
var parser      = require('fast-xml-parser');
var wtf         = require('wtf_wikipedia');


xml.readFile('wiktionary.part_9.xml',function(erro,documento){  

     if(erro){
        console.log("ERRO: " + erro);
     }

    var tObj = parser.getTraversalObj(documento);
    var jsonObj = parser.convertToJson(tObj);
    var t  = wtf(jsonObj.mediawiki.page.revision.text).json();

});

But I don’t know how to query inside the T object that returns me

{ title: undefined,
  categories:
   [ 'Substantivo (Português)',
     'Substantivo (Esperanto)',
     'Cognato (Esperanto)',
     'Substantivo (Franco-Provençal)',
     'Substantivo (Galego)',
     'Substantivo (Ido)' ],
  sections:
   [ { title: '', depth: 0, paragraphs: [Array], templates: [Array] },
     { title: 'Substantivo',
       depth: 0,
       paragraphs: [Array],
       templates: [Array],
       lists: [Array] },
     { title: 'Expressões',
       depth: 1,
       paragraphs: [Array],
       lists: [Array] },
     { title: '', depth: 1, paragraphs: [Array], templates: [Array] },
     { title: 'Tradução',
       depth: 1,
       paragraphs: [Array],
       templates: [Array] },
     { title: '', depth: 0, templates: [Array] },
     { title: 'Ver também', depth: 0 },
     { title: 'No Wikcionário',
       depth: 1,
       paragraphs: [Array],
       templates: [Array],
       lists: [Array] },
     { title: 'Expressões',
       depth: 2,
       paragraphs: [Array],
       templates: [Array],
       lists: [Array] },
     { title: 'Apêndices', depth: 2, paragraphs: [Array] },
     { title: 'Wikisaurus',
       depth: 2,
       paragraphs: [Array],
       templates: [Array] },
     { title: 'Substantivo',
       depth: 0,
       paragraphs: [Array],
       templates: [Array] },
     { title: '', depth: 0, templates: [Array] },
     { title: '', depth: 0, paragraphs: [Array], templates: [Array] },
     { title: 'Ver também', depth: 0 },
     { title: 'No Wikcionário',
       depth: 1,
       paragraphs: [Array],
       templates: [Array] },
     { title: 'Substantivo',
       depth: 0,
       paragraphs: [Array],
       templates: [Array] },
     { title: 'Ver também', depth: 0 },
     { title: 'No Wikcionário',
       depth: 1,
       paragraphs: [Array],
       templates: [Array] },
     { title: 'Substantivo',
       depth: 0,
       paragraphs: [Array],
       templates: [Array] },
     { title: 'Ver também', depth: 0 },
     { title: 'No Wikcionário',
       depth: 1,
       paragraphs: [Array],
       templates: [Array] },
     { title: 'Substantivo',
       depth: 0,
       paragraphs: [Array],
       templates: [Array] },
     { title: 'Ver também', depth: 0 },
     { title: 'No Wikcionário', depth: 1, templates: [Array] } ] }

As you can see, there are several objects and arrays and I don’t know where is what I’m looking for,

  • 3

    Always try to put your code and where you are struggling, what you have tried to do... easier to get help like this here on the forum

  • 1

    Thanks,I created the account today,I don’t know how to use the forum

  • Take a look at the help center of the forum, it will help in the next ones: https://answall.com/help

1 answer

2


What you need is a recursive Function, which searches the various levels of the object. More or less like this:

function findStringInObj(obj, string) {
    for (let i in obj) {
        if (obj[i] instanceof Object) { // Se o elemento for um objeto, procure dentro dele tbm
            findStringInObj(obj[i], string);
        } else if (typeof obj[i] === "string") { // Se for uma string vamos verificar o valor
            if (obj[i].indexOf(string) > -1) {
                console.log(obj[i]);  // Encontrou a string. O que fazer com ela agora?
            }
        }
    }
}

findStringInObj(obj, "Expressões");
  • It shows nothing, on console.log(obj[i]); it would not be to show what found?

  • @mechatroniccomrecycling If you’re not showing anything it’s because I can’t find the string you’re looking for. Make sure it exists in the object?

  • I’m sorry, it’s true,solved here,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.