-1

Good afternoon, please tell me, I have an array consisting of numbers, how can I make the part of the array that stands after the number before each unit be transferred to a new line?

My App.vue:

<template>
  <div id="app">
    <div class="mounth">{{ mounthDays }}</div>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
        mounthDays: [],
    }
  },
  mounted() {
    for (var x = 0; x < 12; x++) {
      for (var i = 1; i <= 31; i++) {
        this.mounthDays.push(i)
      }
    }
    if(this.mounthDays[123] === 31){
      this.mounthDays.splice(59, 3)
      this.mounthDays.splice(120, 1)
      this.mounthDays.splice(181, 1)
      this.mounthDays.splice(273, 1)
      this.mounthDays.splice(334, 1)



    }
    console.log(this.mounthDays)

    }
  }
</script>

enter image description here

That's how I want my array to look

eddyP23
  • 6,420
  • 7
  • 49
  • 87

2 Answers2

0

Instead of having an array of all numbers, make it array of arrays and then print it on separate lines. Something like this should work:

var monthsWithDays = [];

for (var x = 0; x < 12; x++) {
    var days = [];

    for (var i = 1; i <= 31; i++) {
        days.push(i)
    }

    monthsWithDays.push(days);
}

monthsWithDays.forEach(daysArray => console.log(daysArray));

If you want just a string with newlines, then something like this should work:

monthsWithDays.map(daysArray => daysArray.join(",")).join("\n")

EDIT: I now see what you are trying to do with your splice stuff. I don't think you have a right approach here. But the code above could be fixed to do this:

function daysInMonth (month, year) {
    return new Date(year, month, 0).getDate();
}

var monthsWithDays = [];

for (var x = 1; x <= 12; x++) {
    var days = [];

    for (var i = 1; i <= daysInMonth(x, 2019) ; i++) {
        days.push(i)
    }

    monthsWithDays.push(days);
}

monthsWithDays.map(daysArray => daysArray.join(",")).join("\n")

I borrowed the daysInMonth function from another question here

eddyP23
  • 6,420
  • 7
  • 49
  • 87
0

Here's a working example for the current year, which uses a computed property and some date math:

<template>
  <div>
    <div v-for="(month, index) in monthsAndDays" :key="index">{{ month }}</div>
  </div>
</template>

<script>
// https://stackoverflow.com/questions/1184334
const daysInMonth = (month, year) => new Date(year, month, 0).getDate();

// [1..n]
const oneToN = n => Array.from(Array(n), (_, i) => i + 1);

export default {
  computed: {
    monthsAndDays() {
      const year = new Date().getFullYear();
      return oneToN(12).map(i => oneToN(daysInMonth(i, year)));
    },
  },
};
</script>
tony19
  • 125,647
  • 18
  • 229
  • 307
David Weldon
  • 63,632
  • 11
  • 148
  • 146