• Как с помощью JS добавить style тегу < HTML >?

    @Nc_Soft
    document.getElementsByTagName('html')[0].style.height="300px";
    Ответ написан
    Комментировать
  • Как вырезать имя источника с помощью Regex?

    0xD34F
    @0xD34F Куратор тега JavaScript
    str.match(/[^.]+(?=\.[^.]+(\/|$))/)[0]
    Ответ написан
    3 комментария
  • Как распечатать массив после завершения асинхронной операции?

    @Aves
    const axios = require('axios');
    const xml2js = require('xml2js');
    
    const parser = new xml2js.Parser();
    
    const NULL = 0;
    const SECOND = 2;
    
    // Ну концепция данной идеи заключается в том, что если появляется новый канал, то мы его просто добавляем во входной массив,
    const rssChannels = [
      'https://mos.ru/rss',
      'https://lenta.ru/rss/news',
    ];
    
    // Итоговый, агрегационный массив с новостями со всех полученных источников из входного массива rssChannels
    const newsFeed = [];
    
    // Подключение к RSS ленте, получаем XML и приобразуем из него в JS объекты, затем добавляем в агрегационный массив newsFeed
    const getChannelFeed = url =>
      axios.get(url).then(res => {
        return parser.parseStringPromise(res.data)
          .then(res => newsFeed.push(...res.rss.channel[NULL].item))
          .catch(err => console.log('ERROR: Unable to parse received XML'));
      })
        .catch(err => console.log('ERROR: Unable to establish URL connection'));
    
    
    // Заполняем выходной массив контентов
    const someFoo = rssChannels =>
      Promise.all(rssChannels.map(item => getChannelFeed(item)));
    
    
    someFoo(rssChannels).then(item => console.log(newsFeed));

    Вообще, кривовато всё как-то.
    Ответ написан
    1 комментарий
  • Как конкатенировать вложенность массива?

    twobomb
    @twobomb
    Не знаю правильно ли понял ну вот пример

    function ss(&$arr,$path,$cur = 0){
            if(isset($arr[$path[$cur]])){
                $arr[$path[$cur]]["active"] = true;
                if($cur+1 < count($path))
                    ss($arr[$path[$cur]]["children"],$path,$cur+1);
            }
    }

    $arr
    $arr = array (
        array (
            'name' => '-1 пункт',
            'active' => false,
            'children'=> array(),
        ),
        array (
            'name' => '0 пункт',
            'active' => false,
            'children'=> array(),
        ),
         array (
            'name' => '1 пункт',
            'active' => false,
            'children'=> array(
                array(
                    'name' => '1.0 пункт',
                    'active' => false,
                    'children' => array(),
                ),
                 array(
                    'name' => '1.1 пункт',
                    'active' => false,
                    'children' => array(
                        array(
                            'name' => '1.1.0 пункт',
                            'active' => false,
                            'children' => array(),
                        ),
                        array(
                            'name' => '1.1.0 пункт',
                            'active' => false,
                            'children' => array(),
                        )
                    ),
                ),
                array(
                    'name' => '1.2 пункт',
                    'active' => false,
                    'children' => array(),
                ),
            ),
        ),
        array (
            'name' => '2 пункт',
            'active' => false,
            'children'=> array(),
        ),
    );


    $path = [  2,1,1    ];    
        var_dump($arr);//реузльтат до входа
        ss($arr,$path);
        var_dump($arr);//реузльтат после входа

    реузльтат до входа

    array(4) {
    [0]=>
    array(3) {
    ["name"]=>
    string(13) "-1 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [1]=>
    array(3) {
    ["name"]=>
    string(12) "0 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [2]=>
    array(3) {
    ["name"]=>
    string(12) "1 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(3) {
    [0]=>
    array(3) {
    ["name"]=>
    string(14) "1.0 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [1]=>
    array(3) {
    ["name"]=>
    string(14) "1.1 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(2) {
    [0]=>
    array(3) {
    ["name"]=>
    string(16) "1.1.0 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [1]=>
    array(3) {
    ["name"]=>
    string(16) "1.1.0 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    }
    }
    [2]=>
    array(3) {
    ["name"]=>
    string(14) "1.2 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    }
    }
    [3]=>
    array(3) {
    ["name"]=>
    string(12) "2 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    }


    реузльтат после входа

    array(4) {
    [0]=>
    array(3) {
    ["name"]=>
    string(13) "-1 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [1]=>
    array(3) {
    ["name"]=>
    string(12) "0 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [2]=>
    array(3) {
    ["name"]=>
    string(12) "1 пункт"
    ["active"]=>
    bool(true)
    ["children"]=>
    array(3) {
    [0]=>
    array(3) {
    ["name"]=>
    string(14) "1.0 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [1]=>
    array(3) {
    ["name"]=>
    string(14) "1.1 пункт"
    ["active"]=>
    bool(true)
    ["children"]=>
    array(2) {
    [0]=>
    array(3) {
    ["name"]=>
    string(16) "1.1.0 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    [1]=>
    array(3) {
    ["name"]=>
    string(16) "1.1.0 пункт"
    ["active"]=>
    bool(true)
    ["children"]=>
    array(0) {
    }
    }
    }
    }
    [2]=>
    array(3) {
    ["name"]=>
    string(14) "1.2 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    }
    }
    [3]=>
    array(3) {
    ["name"]=>
    string(12) "2 пункт"
    ["active"]=>
    bool(false)
    ["children"]=>
    array(0) {
    }
    }
    }
    Ответ написан
    3 комментария