S
S
Sergey Lyalin2020-08-08 13:27:52
JavaScript
Sergey Lyalin, 2020-08-08 13:27:52

How to structure the data for further work?

I am writing a script (user.js) for myself, for my work. At first I made different autocomplete / templates, and now I want to collect data and make a report on them like:

3 composition:
01.08 - 13pcs
02.08 - 27
03.08 - 17

6 ​​composition:
01.08 - 18
02.08 - 22
03.08 - 32

I upload files to the site, each file on the site has an ID like "23a326bd-180c-462a-94a7-d87561d0e88d" which I get from the DOM .
Also, each file has a "composition" for each of which you need to make a report.
the third mandatory parameter for the file is number / date

I want to save the whole thing in localStorage as an object via JSON.stringify()

Ideally, the end result is the ability to get a table for each composition, in which the number of downloaded files is written by day. Of course, I can write it, but I don’t understand how to find the data correctly.

I get the following entries in localStorage:

key: sostav4
value:
{ 
    "he0075cf-5b2e-42df-a2e0-950ed30146a8": { "date": "1.7", "a40": "А40-135540/2020\n" }, 
    "9d0075cf-5b2e-42df-a2e0-950ed30146a8": { "date": "7.7", "a40": "А40-135540/2020\n" }, 
    "72766e78-1f72-4814-a2c4-69941d4045c3": { "date": "8.8", "a40": "А40-135423/2020\n" }, 
    "0c579c38-d494-4640-9199-595c9fba7868": { "date": "8.8", "a40": "А40-136106/2020\n" } 
}

Accordingly, according to the example, there should be
4 composition:
01.01 - 1
07.08 - 1
08.08 - 2

Here is the part of the script that adds the entries:

let checker = setInterval(() => {
            let saveScanStat = document.querySelector("#b-container > div.b-popup-wrapper.js-popup-wrapper.js-popup-wrapper--upload > div.b-popup.b-popup--blue.b-popup--upload.js-popup--upload.b-popup--edit.js-popup--edit > form > div.b-popup-button.js-upload-submit")// кнопка по которой срабатывает скрипт
            let q = document.querySelector("#chrono_list_content > div.b-chrono-items-container.js-chrono-items-container > div").childElementCount - 2 //Номер определения сверху дива (бывает разныей, зависит от кол-ва доков загруженных после определения)
            let sostav = document.querySelector("#chrono_list_content > div.b-chrono-items-container.js-chrono-items-container > div > div:nth-child(" + q + ") > div.r-col > h2 > span > p:nth-child(1)").textContent.split(" ")[54]
            let docId = document.querySelector("#b-container > div.b-popup-wrapper.js-popup-wrapper.js-popup-wrapper--upload > div.b-popup.b-popup--blue.b-popup--upload.js-popup--upload.b-popup--edit.js-popup--edit > form > input[type=hidden]:nth-child(4)").value
            let a40 = document.querySelector("#b-case-header > ul.crumb.g-ec > li > span").textContent.split(" ")[20]


            //Срабатывает прии НАЖАТИИ на "Сохранить"
            saveScanStat.onclick = () => {
                let now = new Date()
                let dataForLS = JSON.parse(localStorage.getItem(`sostav${sostav}`))

                dataForLS[docId] = {
                    date: `${now.getDate()}.${1 + now.getMonth()}`,
                    a40: `${a40}`
                }

                localStorage.setItem(`sostav${sostav}`, JSON.stringify(dataForLS))

                alert(`Мы записали в ${sostav} состав ${docId} id и ${a40} номер дела `)
            }
        }, 500)
        checker()



That's actually how to search for an object with objects or somehow rewrite the data storage structure?
I'm a little ashamed of my code, please don't scold me, I'm self-taught, only videos and something on the Internet.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Lyalin, 2020-08-09
@Spokik

I decided
that's how I save

saveScanStat.onclick = () => {
                let now = new Date()
                let dataForLS = {}
                dataForLS = JSON.parse(localStorage.getItem(`sostav${sostav}`))
                if (dataForLS == null) {
                    dataForLS = {}
                }
                dataForLS[docId] = {
                    day: `${now.getDate()}`,
                    month: `${1 + now.getMonth()}`,
                    a40: `${a40}`
                }

                localStorage.setItem(`sostav${sostav}`, JSON.stringify(dataForLS))
            }

This is how I read
let dataForLS = {}
                let sostavNumber = document.querySelector("#b-footer > div > div:nth-child(2) > input:nth-child(2)").value
                dataForLS = JSON.parse(localStorage.getItem(`sostav${sostavNumber}`))
                let counter = {}
                let needMonth = Number(document.querySelector("#month").value.substr(5, 7))
                for (let key in dataForLS) {

                    if (dataForLS[key].month == needMonth) {
                        if (counter[dataForLS[key].day] == null) {
                            counter[dataForLS[key].day] = 0
                        }
                        counter[dataForLS[key].day]++
                    }

                }
                console.log(counter);
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question