Answer the question
In order to leave comments, you need to log in
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. data() {
return {
events: [
{ day: 1, start: '08:20 - 08:40', title: 'event'},
{ day: 3, start: '08:40 - 09:00', title: 'event' }
]}
}
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)
}
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
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 questionAsk a Question
731 491 924 answers to any question