D
D
Dmitry2020-12-15 04:05:12
JavaScript
Dmitry, 2020-12-15 04:05:12

How to dynamically change values ​​in an array based on a condition?

Do not hit hard, but again I fell asleep on some nonsense. I don't understand where I went wrong.
There is an object - it comes in json format in theory - from blade to vue.

{ "08:00 - 08:20": [ 0, 0, 0, 0, 0, 0, 0 ], "08:20 - 08:40": [ 0, 0, 0, 0, 0, 0, 0 ],
... etc.
The ultimate task is to substitute events in these slots. It is clear that the main part (instead of 0 I will receive from the server, but not about that now.

Here I did data.
data() {
        return {
           events: [
                { day: 1,  start: '08:20 - 08:40', title: 'event'},
                  { day: 3, start: '08:40 - 09:00', title: 'event' }
            ]}
    }


And the test method is to iterate over and substitute the values.
testslot(e) {
           // console.log('testslot', e.target.getAttribute('data-day'))
                Object.keys(this.calendardata).forEach(key=>{
            // console.log('key',key,Array.isArray(this.calendardata[key],this.calendardata[key]))
              console.log('key',key,this.calendardata[key])

            let event = this.events.find(x => x.start==key) // нахожу event по ключу в общем массиве - хотя это объект
            if (event) {
                this.calendardata[key][event.day]=event; //  "подставляю значение
            }
        });
        console.log(this.calendardata)
        }

everything is output to the console correctly, but the values ​​in the general table do not change. Calendardata is passed from the blade as a prop. Overwritten in data... nothing changes. Where did I not press the save and update button?) Object.assign - does not submit (
08:20 - 08:40: Array(7)
0: 0
1:
day: (...)
start: (...)
title: (...)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gololobov, 2020-12-15
@DKWLB

Read about reactivity in vue. Everything is written in the documentation in a very accessible way. https://ru.vuejs.org/v2/guide/reactivity.html
To replace values ​​in an array, you can use at least the following methods:
1) Change the value through splice: 2) Change the value through vue set: 3) Change the entire array at once , for example via map:
this.array.splice(oldElementIndex, 1, newElement)
this.$set(this.array, oldElementIndex, newElement)

this.array = this.array.map(el => {
    el.someParam = 1
     return el
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question