S
S
ski282020-01-21 19:01:39
JavaScript
ski28, 2020-01-21 19:01:39

How to pass a pointer to an array as text?

Good afternoon.
Help to pass pointer to multidimensional array as text. Or tell me how to implement this function.
The essence of the question is this: there are multidimensional arrays containing objects. If during the search the value of the content was found, then go to the next level and search in it. Now I will try to explain with an example in more detail.
Let's say we have the line "Dima works at home";
I want to split this string in a tree view.

[{
    "id": "д",
    "r": [{
        "id": "о",
        "r": [{
            "id": "м",
            "r": [{
                "id": "а",
                "r": []
            }]
        }]
    }, {
        "id": "и",
        "r": [{
            "id": "м",
            "r": [{
                "id": "а",
                "r": []
            }]
        }]
    }]
}, {
    "id": "р",
    "r": [{
        "id": "а",
        "r": [{
            "id": "б",
            "r": [{
                "id": "о",
                "r": []
            }]
        }]
    }]
}]

I ran into a problem that I don't know how to pass a pointer to iterate over the next array if a match is found on my level.
I'm starting to search manually.
var result = [];
var s = "Дома Дима работает";
var t = s.split(" ");
for (let i = 0; i < t.length; i++) {
    for (let a = 0; a < t[i].length; a++) {

        //Добавляем первую букву
        if (a == 0) {
            var id = result.findIndex(item => item.id === t[i][a]);
            if (id == -1) {
                console.log("if 0 Массив 1");
                result.push({
                    id: t[i][a],
                    r: []
                });
            }
        }

        //Добавляем вторую букву                    
        if (a == 1) {
            var id = result.findIndex(item => item.id === t[i][a - 1]);

            if (id !== -1) {
                var id1 = result[id].r.findIndex(item => item.id === t[i][a]);

                if (id1 == -1) {
                    result[id].r.push({
                        id: t[i][a],
                        r: []
                    });
                }
            }

        }

        //Добавляем третью букву
        if (a == 2) {
            //Ищем первую букву
            var id = result.findIndex(item => item.id === t[i][a - 2]);

            if (id !== -1) {
                //Ищем вторую букву
                var id1 = result[id].r.findIndex(item => item.id === t[i][a - 1]);

                if (id1 !== -1) {
                    var id2 = result[id].r[id1].r.findIndex(item => item.id === t[i][a]);

                    if (id2 == -1) {
                        result[id].r[id1].r.push({
                            id: t[i][a],
                            r: []
                        });
                    }
                }
            }
        }

        //Добавляем четвертую букву
        if (a == 3) {

            //Ищем первую букву
            var id = result.findIndex(item => item.id === t[i][a - 3]);

            if (id !== -1) {
                //Ищем вторую букву
                var id1 = result[id].r.findIndex(item => item.id === t[i][a - 2]);

                if (id1 !== -1) {
                    var id2 = result[id].r[id1].r.findIndex(item => item.id === t[i][a - 1]);
                    if (id2 !== -1) {
                        var id3 = result[id].r[id1].r[id2].r.findIndex(item => item.id === t[i][a]);
                        if (id3 == -1) {
                            result[id].r[id1].r[id2].r.push({
                                id: t[i][a],
                                r: []
                            });
                        }
                    }
                }
            }
        }

    }
}

console.log(result);
console.log(JSON.stringify(result));

I can not understand how this code can be written regardless of the length of the word? I imagine that if you sort through the function and pass it a pointer to an array where you need to search in the form of text, it would solve the problem
function s(data){
       data.findIndex(item => item.id === t[i][a]);
}
//И тут передавать ссылку на определенный массив в виде текста решило бы проблему. 
s("result[id].r[id1].r[id2].r");

I'm waiting for your help)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
ski28, 2020-01-23
@ski28

Made via eval

K
Karpion, 2020-01-22
@Karpion

I did not understand the principle of tree organization. There is no "d" in the second branch - is it because the first letter of the first two words is the same?
What are you looking for? Some line? What for?
Give a non-degenerate example of searching for some string and the result of the search.
Well, I can suggest creating an array of pointers. Those. found the first letter - in the zero element of the array we write its address (link to it) in the tree. We found the second letter - we write its address in the second element of the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question