A
A
ambal2452020-09-22 10:06:40
JavaScript
ambal245, 2020-09-22 10:06:40

How to create js array with data from comments?

There is a page on the Yandex market with tree-like comments.

spoiler
market.yandex.ru/product--noutbuk-lenovo-ideapad-l340-17/530360084/reviews?onstock=0
It is not possible to create a multidimensional array in js of this kind.
Array (
[comment] => array(
                [0] =>  Первый комментарий
                [comment1] => array(
                                  [0] => Первый Ответ первого уровня
                                  [comment2] => array (  
                                                    [0] => Первый Ответ второго уровня
                                                    [1] => Второй Ответ второго уровня )
                                 [1] => Второй Ответ первого уровня
                                 [comment2] => array (
                                                   [0] => Первый Ответ второго уровня
                                                   [1] => Второй Ответ второго уровня )
                [1] =>  Второй комментарий
                [comment1] =>  array(
                                  [0] => Первый Ответ первого уровня второго комментария
                                  [comment2] => array (  
                                                    [0] => Певый Ответ второго уровня
                                                    [1] => Второй Ответ второго уровня )
                                 [1] => Второй Ответ первого уровня второго комментария
                                 [comment2] => array (
                                                   [0] => Первый Ответ второго уровня
                                                   [1] => Второй Ответ второго уровня )
)
)
)


This is how I get all the comments from the page through the browser console and try to create an array using the push method
var sections = [];
$('._3K8Ed1m6dJ').each(function(){
  var a = {comment: $(this).find('._3IXczk7DdZ').text()};
    var b = {comment1: $(this).find('._3TYVUIDY0c ._1DcXedarfT').text()};
    var c = {comment2: $(this).find('._1z_MwzAsrC ._1mA15wj--q').text()};
sections.push(a,[b,[c]]); // Пробывал много разных вариантов
});
copy(JSON.stringify(sections));

But I don't get exactly what I want. Tell me how to convert the received JSON data with the desired structure above

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2020-09-22
@Stalker_RED

nYKjcE2.png
Such a structure is not possible.
When inserting a second value with a key , the comment1previous value will be replaced.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question