javascript - 尝试在 JavaScript 中递归查找属性

标签 javascript recursion array.prototype.map

**UPDATE**

/* This is my file in Node: */

var Crawler = require("crawler");
const fs = require('fs');

function iterObj(obj) {
let arr = []
let objValues = Object.values(obj);
// use var to avoid additonal heap overhead of new variable for each iteration
for (var i = 0, len = objValues.length; i < len; i++) {
    if (objValues[i] !== null) {
        if (objValues[i].children || objValues[i].next || objValues[i].prev || objValues[i].parent) {
            delete objValues[i].children;
            delete objValues[i].next;
            delete objValues[i].prev;
            delete objValues[i].parent;
        }
        if (objValues[i].src) { arr.push({ src: objValues[i].src }) }

        console.log("contents of arr on line 29 ", arr);
        if (typeof objValues[i] === "object" && !Array.isArray(objValues[i])) {
            arr.push(...iterObj(objValues[i]));
        }
    }
}
return arr;
}



var c = new Crawler({
maxConnections: 10,
// This will be called for each crawled page
callback: function(error, res, done) {
    if (error) {
        console.log(error);
    } else {
        var $ = res.$;
        // $ is Cheerio by default
        //a lean implementation of core jQuery designed specifically for the server;
        var img = $(`img`);

        console.log('img', img);
        console.log('iterObj(img)', iterObj(img))


        // fs.writeFile('fragrance.js', img, (err) => {
        //     if (err) throw err;
        //     console.log(`img`, img);
        //     console.log(`iterObj(img)`, iterObj(img));


        //     console.log('Data written to file.')
        // })

    }
    done();
}
});


c.queue('https://www.elizabetharden.com/fragrance/')


And this is the data, a javascript object.

img initialize {
  '0':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         '/on/demandware.static/Sites-ElizabethArden-Site/-/default/dwbde506b9/images/ELIZABETH_ARDEN_LOGO_640x90.png',
        alt: 'Elizabeth Arden' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'picture',
        attribs: {},
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '1':
   { type: 'tag',
     name: 'img',
     attribs:
      { alt: 'Fragrance Collection',
        src:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dwd973092c/images/category-landing/D_White_Tea_Fragrance_Page_Banner-bg.jpg',
        srcset:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dwd973092c/images/category-landing/D_White_Tea_Fragrance_Page_Banner-bg.jpg, https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw62e6fa40/images/category-landing/D_White_Tea_Fragrance_Page_Banner@2x-bg.jpg 2x' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'picture',
        attribs: {},
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '2':
   { type: 'tag',
     name: 'img',
     attribs:
      { alt: 'My Fifth Avenue Fragrance',
        src:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw255d9a03/images/category-landing/myfifthavenue_1100x1100.jpg',
        srcset:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw255d9a03/images/category-landing/myfifthavenue_1100x1100.jpg' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'picture',
        attribs: {},
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '3':
   { type: 'tag',
     name: 'img',
     attribs:
      { alt: 'Elizabeth Arden Green Tea Perfume',
        src:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw8c63d4b5/images/category-landing/green-tea_1100x1100.jpg',
        srcset:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw8c63d4b5/images/category-landing/green-tea_1100x1100.jpg' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'picture',
        attribs: {},
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '4':
   { type: 'tag',
     name: 'img',
     attribs:
      { alt: 'Elizabeth Arden White Tea Perfume',
        src:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw489093d9/images/category-landing/white-tea_1100x1100.jpg',
        srcset:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw489093d9/images/category-landing/white-tea_1100x1100.jpg' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'picture',
        attribs: {},
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '5':
   { type: 'tag',
     name: 'img',
     attribs:
      { alt: 'Skincare Diagnostic',
        src:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw51ed6763/images/category-landing/red-door_1100x1100.jpg',
        srcset:
         'https://www.elizabetharden.com/on/demandware.static/-/Sites-ElizabethArden-Library/default/dw51ed6763/images/category-landing/red-door_1100x1100.jpg' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'picture',
        attribs: {},
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '6':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwc531e5e7/images/2104_A0115075.jpg?sw=550&sh=550&sm=fit',
        alt: 'My Fifth Avenue Eau de Parfum, , large',
        class: 'desktop-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: null,
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '7':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwc531e5e7/images/2104_A0115075.jpg?sw=550&sh=550&sm=fit',
        alt: 'My Fifth Avenue Eau de Parfum, , large',
        class: 'ipad-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '8':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwc531e5e7/images/2104_A0115075.jpg?sw=135&sh=136&sm=fit',
        alt: 'My Fifth Avenue Eau de Parfum, , large',
        class: 'iphone-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '9':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwbb9e72e0/images/1003A0106574.jpg?sw=550&sh=550&sm=fit',
        alt: 'Elizabeth Arden White Tea Eau de Toilette Spray, , large',
        class: 'desktop-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: null,
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '10':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwbb9e72e0/images/1003A0106574.jpg?sw=550&sh=550&sm=fit',
        alt: 'Elizabeth Arden White Tea Eau de Toilette Spray, , large',
        class: 'ipad-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '11':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwbb9e72e0/images/1003A0106574.jpg?sw=135&sh=136&sm=fit',
        alt: 'Elizabeth Arden White Tea Eau de Toilette Spray, , large',
        class: 'iphone-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '12':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw55f80ff7/images/100326800.jpg?sw=550&sh=550&sm=fit',
        alt: 'Green Tea Scent Spray, , large',
        class: 'desktop-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: null,
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '13':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw55f80ff7/images/100326800.jpg?sw=550&sh=550&sm=fit',
        alt: 'Green Tea Scent Spray, , large',
        class: 'ipad-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '14':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw55f80ff7/images/100326800.jpg?sw=135&sh=136&sm=fit',
        alt: 'Green Tea Scent Spray, , large',
        class: 'iphone-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '15':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw0dcf8cae/images/10034372400.jpg?sw=550&sh=550&sm=fit',
        alt: 'Green Tea Honey Drops Body Cream, , large',
        class: 'desktop-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: null,
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '16':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw0dcf8cae/images/10034372400.jpg?sw=550&sh=550&sm=fit',
        alt: 'Green Tea Honey Drops Body Cream, , large',
        class: 'ipad-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '17':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw0dcf8cae/images/10034372400.jpg?sw=135&sh=136&sm=fit',
        alt: 'Green Tea Honey Drops Body Cream, , large',
        class: 'iphone-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '18':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwc687b1bb/images/2104-A0119537-Red-Door-Fragrance-3.3oz-4-Piece-Set.jpg?sw=550&sh=550&sm=fit',
        alt: 'Red Door 3.3oz Eau De Toilette 4-Piece Set, , large',
        class: 'desktop-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: null,
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '19':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwc687b1bb/images/2104-A0119537-Red-Door-Fragrance-3.3oz-4-Piece-Set.jpg?sw=550&sh=550&sm=fit',
        alt: 'Red Door 3.3oz Eau De Toilette 4-Piece Set, , large',
        class: 'ipad-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '20':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dwc687b1bb/images/2104-A0119537-Red-Door-Fragrance-3.3oz-4-Piece-Set.jpg?sw=135&sh=136&sm=fit',
        alt: 'Red Door 3.3oz Eau De Toilette 4-Piece Set, , large',
        class: 'iphone-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '21':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw86b39b0f/images/2104_Pomegranate.jpg?sw=550&sh=550&sm=fit',
        alt: 'Green Tea Pomegranate  Eau De Toilette Spray, , large',
        class: 'desktop-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: null,
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '22':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw86b39b0f/images/2104_Pomegranate.jpg?sw=550&sh=550&sm=fit',
        alt: 'Green Tea Pomegranate  Eau De Toilette Spray, , large',
        class: 'ipad-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: [Object],
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  '23':
   { type: 'tag',
     name: 'img',
     attribs:
      { src:
         'https://www.elizabetharden.com/dw/image/v2/AAHP_PRD/on/demandware.static/-/Sites-elizabetharden-master-catalog/default/dw86b39b0f/images/2104_Pomegranate.jpg?sw=135&sh=136&sm=fit',
        alt: 'Green Tea Pomegranate  Eau De Toilette Spray, , large',
        class: 'iphone-only' },
     children: [],
     next:
      { data: '\n',
        type: 'text',
        next: null,
        prev: [Circular],
        parent: [Object] },
     prev:
      { data: '\n',
        type: 'text',
        next: [Circular],
        prev: [Object],
        parent: [Object] },
     parent:
      { type: 'tag',
        name: 'a',
        attribs: [Object],
        children: [Array],
        next: [Object],
        prev: [Object],
        parent: [Object] } },
  options:
   { normalizeWhitespace: false,
     xmlMode: false,
     decodeEntities: true,
     withDomLvl1: true },
  _root:
   initialize {
     '0':
      { type: 'root',
        name: 'root',
        attribs: {},
        children: [Array],
        next: null,
        prev: null,
        parent: null },
     options:
      { normalizeWhitespace: false,
        xmlMode: false,
        decodeEntities: true,
        withDomLvl1: true },
     length: 1,
     _root: [Circular] },
  length: 24,
  prevObject:
   initialize {
     '0':
      { type: 'root',
        name: 'root',
        attribs: {},
        children: [Array],
        next: null,
        prev: null,
        parent: null },
     options:
      { normalizeWhitespace: false,
        xmlMode: false,
        decodeEntities: true,
        withDomLvl1: true },
     length: 1,
     _root: [Circular] } }

var initialize = {
      '0': {   
         type: 'tag',
         name: 'img',
         attribs: { 
            src: 'path/to/file.png',
            alt: 'SEO is good' 
         },
       },
      '1': { 
         type: 'tag',
         name: 'img',
         attribs: { 
             src: 'path/to/another/image.jpg',
            alt: 'SEO is GREAT' 

         }
       }

我有一个对象,想要递归遍历,直到获得我想要的属性和值。

这不是你会做的吗?

function iterObj(obj) {
  var arr = []
  arr.push(obj)
  return arr.map(obj => {
    var newObj = {}
    if (obj !== null && typeof obj === "object") {
      iterObj(obj);
    }
    newObj[obj.src] = obj.src;
    return newObj;
  })
}

所以不应该 iterObj(initialize) 产量:

[{src: 'path/to/file.png'}, {src: 'path/to/another/image.jpg'}]

更新:

可能有助于包含我使用它的上下文:

下面是我在节点中使用的scraper模块。

var Crawler = require("crawler");
const fs = require('fs');
   
function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}

function iterObj(obj) {
    var arr = []
    var isNotNull = (value) => typeof value !== "object" && value !== null

    for (var value of Object.values(obj)) {
        console.log("value line 18", value);

        if (value !== null) {
            if (value.src) { arr.push({ src: value.src }) }
            if (typeof value === "object" && !Array.isArray(value)) {
                console.log("value line 23", value);

                arr.push(...iterObj(value));
            }
        }
    }
    return arr;
}



var c = new Crawler({
    maxConnections: 10,
    // This will be called for each crawled page
    callback: function(error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;
            // $ is Cheerio by default
            //a lean implementation of core jQuery designed specifically for the server;
            var img = $(`img`);
            fs.writeFile('fragrance.js', img, (err) => {
                if (err) throw err;
                console.log(`img`, img);
                console.log(`iterObj(obj) ${iterObj(img)}`)
                    // console.log(`IsJsonString(img)`, IsJsonString(img[0]));


                console.log('Data written to file.')
            })

        }
        done();
    }
});

c.queue('https://www.somesite.com/someendpoint/')

最佳答案

为了检查对象的每个属性,您需要获取对象属性和/或其各自值的列表,可以使用 Object.keys() , Object.values()Object.entries() 。从那里,您需要检查每个值是否满足以下条件:

1) 如果它有 src 属性,则推送该属性并继续

2) 如果它是一个对象且不为 null,则递归该对象以检查它或其子对象是否具有 src 属性

然后将递归调用的结果连接到原始数组并返回最终结果。这是您缺少的关键部分之一。您正在进行递归调用,但没有对它们执行任何操作。

您无法使用 .map 的原因就像您一样,是因为您没有从对象一对一返回 src。一个对象可以包含任意数量的子 src 属性,也可以包含 0 个。您需要手动构建结果列表。

这是您正在尝试执行的操作的示例:

var initialize = {
  '0': {   
      type: 'tag',
      name: 'img',
      attribs: { 
        src: 'path/to/file.png',
        alt: 'SEO is good',
        self: null,
        parent: null,
        top: null
      },
      next: {
        prev: null
      },
      prev: {
        next: null
      }
    },
  '1': { 
      type: 'tag',
      name: 'img',
      attribs: { 
          src: 'path/to/another/image.jpg',
        alt: 'SEO is GREAT' 

      }
    }
}

// add circular references for testing
initialize[0].attribs.self = initialize[0].attribs;
initialize[0].attribs.parent = initialize[0];
initialize[0].attribs.top = initialize;
initialize[0].next.prev = initialize[0].prev;
initialize[0].prev.next = initialize[0].next;

function iterObj(nestedObj) {
  // keep a list of object references to prevent circular references at any level
  let objReferences = [];
  function rHelper(obj) {
    let arr = []
    let objValues = Object.values(obj);
    // use var to avoid additonal heap overhead of new variable for each iteration
    for (var i = 0; i < objValues.length; i++) {
      let val = objValues[i];
      // check if non null and not a reference we've already seen before (AKA circular reference)
      if (val !== null && objReferences.indexOf(val) === -1) {
        if (val.src) { arr.push({ src: val.src }) }
        if (typeof val === "object" && !Array.isArray(val)) {
          // since we're checking this object, add reference to list to make sure it doesn't get
          // referenced by any of its children or siblings
          objReferences.push(val);
          arr.push(...rHelper(val));
        }
      }
    }
    return arr;
  }

  return rHelper(nestedObj);
}

console.log(iterObj(initialize));

关于javascript - 尝试在 JavaScript 中递归查找属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59869285/

相关文章:

javascript - .map 内的排序会影响原始数组而不是返回新数组

javascript - 验证 ember.js 中动态段的格式

javascript - 为什么我不能在单词边界旁边使用重音字符?

reactjs - 从 API React 映射对象数组

file - 如何使用命令行从 linux 服务器递归 ftp 仅某些文件类型?

java - 根据业务标准压缩文件夹/文件

reactjs - 限制 React 中映射值的字符长度

javascript - 调整表格标题/行的大小以在表格更改时匹配表格中的第一行

javascript - struts2 jquery 表仅显示在第一个选项卡上

java - 使用异常从深度递归合法返回吗?