bemdev
@bemdev

Как избежать такой вложенности циклов?

Добрый день!

Получаю AST json-to-ast дерево и начинаю его проверять по своим правилам и для обхода получается какая то дичь) как этого избежать?

array.forEach(content => {
                        if (content.key.value == 'content') {
                            content.value.children.forEach((text, i, context) => {
                                text.children.forEach((mods) => {
                                    if (mods.key.value == 'mods') {
                                        if (mods.value.children[0].key.value == 'size') {
                                            arr.push(
                                                {
                                                    value: mods.value.children[0].value.value,
                                                    loc: mods.value.children[0].loc
                                                }
                                            );
                                        }
                                    }
                                });
                            });
                        }
                    });


{
        type: 'Object',
        children: [
          { type: 'Property', key: [Object], value: [Object], loc: [Object] },
          { type: 'Property', key: [Object], value: [Object], loc: [Object] }
        ],
        loc: {
          start: { line: 1, column: 1, offset: 0 },
          end: { line: 7, column: 6, offset: 184 },
          source: null
        }
      }

{"type":"Object","children":[{"type":"Property","key":{"type":"Identifier","value":"block","raw":"\"block\"","loc":{"start":{"line":2,"column":9,"offset":10},"end":{"line":2,"column":16,"offset":17},"source":null}},"value":{"type":"Literal","value":"warning","raw":"\"warning\"","loc":{"start":{"line":2,"column":18,"offset":19},"end":{"line":2,"column":27,"offset":28},"source":null}},"loc":{"start":{"line":2,"column":9,"offset":10},"end":{"line":2,"column":27,"offset":28},"source":null}},{"type":"Property","key":{"type":"Identifier","value":"content","raw":"\"content\"","loc":{"start":{"line":3,"column":9,"offset":38},"end":{"line":3,"column":18,"offset":47},"source":null}},"value":{"type":"Array","children":[{"type":"Object","children":[{"type":"Property","key":{"type":"Identifier","value":"block","raw":"\"block\"","loc":{"start":{"line":4,"column":15,"offset":65},"end":{"line":4,"column":22,"offset":72},"source":null}},"value":{"type":"Literal","value":"text","raw":"\"text\"","loc":{"start":{"line":4,"column":24,"offset":74},"end":{"line":4,"column":30,"offset":80},"source":null}},"loc":{"start":{"line":4,"column":15,"offset":65},"end":{"line":4,"column":30,"offset":80},"source":null}},{"type":"Property","key":{"type":"Identifier","value":"mods","raw":"\"mods\"","loc":{"start":{"line":4,"column":32,"offset":82},"end":{"line":4,"column":38,"offset":88},"source":null}},"value":{"type":"Object","children":[{"type":"Property","key":{"type":"Identifier","value":"size","raw":"\"size\"","loc":{"start":{"line":4,"column":42,"offset":92},"end":{"line":4,"column":48,"offset":98},"source":null}},"value":{"type":"Literal","value":"l","raw":"\"l\"","loc":{"start":{"line":4,"column":50,"offset":100},"end":{"line":4,"column":53,"offset":103},"source":null}},"loc":{"start":{"line":4,"column":42,"offset":92},"end":{"line":4,"column":53,"offset":103},"source":null}}],"loc":{"start":{"line":4,"column":40,"offset":90},"end":{"line":4,"column":55,"offset":105},"source":null}},"loc":{"start":{"line":4,"column":32,"offset":82},"end":{"line":4,"column":55,"offset":105},"source":null}}],"loc":{"start":{"line":4,"column":13,"offset":63},"end":{"line":4,"column":57,"offset":107},"source":null}},{"type":"Object","children":[{"type":"Property","key":{"type":"Identifier","value":"block","raw":"\"block\"","loc":{"start":{"line":5,"column":15,"offset":123},"end":{"line":5,"column":22,"offset":130},"source":null}},"value":{"type":"Literal","value":"button","raw":"\"button\"","loc":{"start":{"line":5,"column":24,"offset":132},"end":{"line":5,"column":32,"offset":140},"source":null}},"loc":{"start":{"line":5,"column":15,"offset":123},"end":{"line":5,"column":32,"offset":140},"source":null}},{"type":"Property","key":{"type":"Identifier","value":"mods","raw":"\"mods\"","loc":{"start":{"line":5,"column":34,"offset":142},"end":{"line":5,"column":40,"offset":148},"source":null}},"value":{"type":"Object","children":[{"type":"Property","key":{"type":"Identifier","value":"size","raw":"\"size\"","loc":{"start":{"line":5,"column":44,"offset":152},"end":{"line":5,"column":50,"offset":158},"source":null}},"value":{"type":"Literal","value":"xl","raw":"\"xl\"","loc":{"start":{"line":5,"column":52,"offset":160},"end":{"line":5,"column":56,"offset":164},"source":null}},"loc":{"start":{"line":5,"column":44,"offset":152},"end":{"line":5,"column":56,"offset":164},"source":null}}],"loc":{"start":{"line":5,"column":42,"offset":150},"end":{"line":5,"column":58,"offset":166},"source":null}},"loc":{"start":{"line":5,"column":34,"offset":142},"end":{"line":5,"column":58,"offset":166},"source":null}}],"loc":{"start":{"line":5,"column":13,"offset":121},"end":{"line":5,"column":60,"offset":168},"source":null}}],"loc":{"start":{"line":3,"column":20,"offset":49},"end":{"line":6,"column":10,"offset":178},"source":null}},"loc":{"start":{"line":3,"column":9,"offset":38},"end":{"line":6,"column":10,"offset":178},"source":null}}],"loc":{"start":{"line":1,"column":1,"offset":0},"end":{"line":7,"column":6,"offset":184},"source":null}}
  • Вопрос задан
  • 85 просмотров
Пригласить эксперта
Ответы на вопрос 1
@mn3m0n1c_3n3m1
Разделяй и властвуй:
const allChildren = getAllChildren(array);
const textChildren = getTextChildren(allChildren);
const arr = getResultArr(textChildren);

function getAllChildren(array) {
    const allChildren = [];
    
    for (const {key, value} of array) {
        if (key.value !== 'content')
            continue;

        allChildren.push(...value.children);
    }
    
    return allChildren;
}

function getTextChildren(allChildren) {
    const textChildren = [];

    for (const text of allChildren) {
        textChildren.push(...text.children);
    }
    
    return textChildren;
}

function getResultArr(textChildren) {
    const arr = []
    
    for (const {key, value} of textChildren) {
        if (key.value !== 'mods')
            continue;

        const [first] = value.children;
        const {key, value, loc} = first;
        
        if (key.value !== 'size')
            continue;

        arr.push({
            loc,
            value: value.value,
        });
    }
    
    return arr;
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы