Place things in a specific iteration element in Vue












1















Let's say I create 20 divs using a v-for.
Then let's say I have a lot of data in an array, and each unordered element of this array belongs in one of the 20 divs (they have an index or something that tells where they belong, but not necessarily all of them).



The simple way to do this would be to iterate over the array on each one of the v-for divs and check if the indexes match and if they do, render them. Something with a v-if element_index == for_index. But this is really inefficient because if there's a lot of data and the v-for has a lot of iterations, this grows a lot for a simple webpage.



Is there a way to do the opposite of this? So first generate the 20 divs, then run through the data array and one by one insert them where they belong?










share|improve this question























  • could you share a tried code?

    – Boussadjra Brahim
    Nov 22 '18 at 22:22











  • I haven't really tried because I only know what the non optimal solution to this would be. I actually have no idea if my idea is possible. Maybe I should do it in another way

    – Myntekt
    Nov 22 '18 at 22:23
















1















Let's say I create 20 divs using a v-for.
Then let's say I have a lot of data in an array, and each unordered element of this array belongs in one of the 20 divs (they have an index or something that tells where they belong, but not necessarily all of them).



The simple way to do this would be to iterate over the array on each one of the v-for divs and check if the indexes match and if they do, render them. Something with a v-if element_index == for_index. But this is really inefficient because if there's a lot of data and the v-for has a lot of iterations, this grows a lot for a simple webpage.



Is there a way to do the opposite of this? So first generate the 20 divs, then run through the data array and one by one insert them where they belong?










share|improve this question























  • could you share a tried code?

    – Boussadjra Brahim
    Nov 22 '18 at 22:22











  • I haven't really tried because I only know what the non optimal solution to this would be. I actually have no idea if my idea is possible. Maybe I should do it in another way

    – Myntekt
    Nov 22 '18 at 22:23














1












1








1








Let's say I create 20 divs using a v-for.
Then let's say I have a lot of data in an array, and each unordered element of this array belongs in one of the 20 divs (they have an index or something that tells where they belong, but not necessarily all of them).



The simple way to do this would be to iterate over the array on each one of the v-for divs and check if the indexes match and if they do, render them. Something with a v-if element_index == for_index. But this is really inefficient because if there's a lot of data and the v-for has a lot of iterations, this grows a lot for a simple webpage.



Is there a way to do the opposite of this? So first generate the 20 divs, then run through the data array and one by one insert them where they belong?










share|improve this question














Let's say I create 20 divs using a v-for.
Then let's say I have a lot of data in an array, and each unordered element of this array belongs in one of the 20 divs (they have an index or something that tells where they belong, but not necessarily all of them).



The simple way to do this would be to iterate over the array on each one of the v-for divs and check if the indexes match and if they do, render them. Something with a v-if element_index == for_index. But this is really inefficient because if there's a lot of data and the v-for has a lot of iterations, this grows a lot for a simple webpage.



Is there a way to do the opposite of this? So first generate the 20 divs, then run through the data array and one by one insert them where they belong?







html vue.js nuxt.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 22:14









MyntektMyntekt

1501113




1501113













  • could you share a tried code?

    – Boussadjra Brahim
    Nov 22 '18 at 22:22











  • I haven't really tried because I only know what the non optimal solution to this would be. I actually have no idea if my idea is possible. Maybe I should do it in another way

    – Myntekt
    Nov 22 '18 at 22:23



















  • could you share a tried code?

    – Boussadjra Brahim
    Nov 22 '18 at 22:22











  • I haven't really tried because I only know what the non optimal solution to this would be. I actually have no idea if my idea is possible. Maybe I should do it in another way

    – Myntekt
    Nov 22 '18 at 22:23

















could you share a tried code?

– Boussadjra Brahim
Nov 22 '18 at 22:22





could you share a tried code?

– Boussadjra Brahim
Nov 22 '18 at 22:22













I haven't really tried because I only know what the non optimal solution to this would be. I actually have no idea if my idea is possible. Maybe I should do it in another way

– Myntekt
Nov 22 '18 at 22:23





I haven't really tried because I only know what the non optimal solution to this would be. I actually have no idea if my idea is possible. Maybe I should do it in another way

– Myntekt
Nov 22 '18 at 22:23












1 Answer
1






active

oldest

votes


















1














I would use reduce to create a "bucket" object keyed by the grouping indexes.




The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.




Example






new Vue({
el: '#app',
data() {
return {
dataArray:
}
},
computed: {
bucket () {
return this.dataArray.reduce((obj, item) => {
if (!obj.hasOwnProperty(item.index)) {
Object.assign(obj, {
[item.index]: [item]
})
} else {
obj[item.index].push(item)
}
return obj
}, {})
}
},
created() {
for (i = 0; i < 10; i++) {
this.addData()
}
},
methods: {
addData() {
this.dataArray.push({
index: Math.ceil(Math.random() * 3),
name: Math.random().toString(36).substring(7)
})
},
removeData() {
this.dataArray = this.dataArray.slice(1)
}
}
})

<div id="app">
<button @click="addData(true)">Add Data</button>
<button @click="removeData">Remove Data</button>
<div v-for="key in Object.keys(bucket)" :key="key">
<h3 style="font-family: monospace">Bucket {{ key }} ({{ bucket[key].length }} items)</h3>
<p v-for="(item, index) in bucket[key]" :key="item.name" style="font-family: monospace; border-bottom: 1px solid rgba(96,125,139,.1)">{{ index + 1 }}. {{ item.name }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.17/browser.min.js"></script></script>








share|improve this answer


























  • Well I have no idea what that does and how it works. But in that approach you can't dynamically add more data for the array, right? Or even dynamically increase the size the div "list"

    – Myntekt
    Nov 22 '18 at 22:29











  • You sure can. Computed properties react to changes in the underlying data (ie this.bigArrayOfData)

    – Phil
    Nov 22 '18 at 22:35











  • @Myntekt I've updated the answer to demonstrate Phil's comment.

    – DigitalDrifter
    Nov 23 '18 at 2:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438552%2fplace-things-in-a-specific-iteration-element-in-vue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I would use reduce to create a "bucket" object keyed by the grouping indexes.




The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.




Example






new Vue({
el: '#app',
data() {
return {
dataArray:
}
},
computed: {
bucket () {
return this.dataArray.reduce((obj, item) => {
if (!obj.hasOwnProperty(item.index)) {
Object.assign(obj, {
[item.index]: [item]
})
} else {
obj[item.index].push(item)
}
return obj
}, {})
}
},
created() {
for (i = 0; i < 10; i++) {
this.addData()
}
},
methods: {
addData() {
this.dataArray.push({
index: Math.ceil(Math.random() * 3),
name: Math.random().toString(36).substring(7)
})
},
removeData() {
this.dataArray = this.dataArray.slice(1)
}
}
})

<div id="app">
<button @click="addData(true)">Add Data</button>
<button @click="removeData">Remove Data</button>
<div v-for="key in Object.keys(bucket)" :key="key">
<h3 style="font-family: monospace">Bucket {{ key }} ({{ bucket[key].length }} items)</h3>
<p v-for="(item, index) in bucket[key]" :key="item.name" style="font-family: monospace; border-bottom: 1px solid rgba(96,125,139,.1)">{{ index + 1 }}. {{ item.name }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.17/browser.min.js"></script></script>








share|improve this answer


























  • Well I have no idea what that does and how it works. But in that approach you can't dynamically add more data for the array, right? Or even dynamically increase the size the div "list"

    – Myntekt
    Nov 22 '18 at 22:29











  • You sure can. Computed properties react to changes in the underlying data (ie this.bigArrayOfData)

    – Phil
    Nov 22 '18 at 22:35











  • @Myntekt I've updated the answer to demonstrate Phil's comment.

    – DigitalDrifter
    Nov 23 '18 at 2:57
















1














I would use reduce to create a "bucket" object keyed by the grouping indexes.




The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.




Example






new Vue({
el: '#app',
data() {
return {
dataArray:
}
},
computed: {
bucket () {
return this.dataArray.reduce((obj, item) => {
if (!obj.hasOwnProperty(item.index)) {
Object.assign(obj, {
[item.index]: [item]
})
} else {
obj[item.index].push(item)
}
return obj
}, {})
}
},
created() {
for (i = 0; i < 10; i++) {
this.addData()
}
},
methods: {
addData() {
this.dataArray.push({
index: Math.ceil(Math.random() * 3),
name: Math.random().toString(36).substring(7)
})
},
removeData() {
this.dataArray = this.dataArray.slice(1)
}
}
})

<div id="app">
<button @click="addData(true)">Add Data</button>
<button @click="removeData">Remove Data</button>
<div v-for="key in Object.keys(bucket)" :key="key">
<h3 style="font-family: monospace">Bucket {{ key }} ({{ bucket[key].length }} items)</h3>
<p v-for="(item, index) in bucket[key]" :key="item.name" style="font-family: monospace; border-bottom: 1px solid rgba(96,125,139,.1)">{{ index + 1 }}. {{ item.name }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.17/browser.min.js"></script></script>








share|improve this answer


























  • Well I have no idea what that does and how it works. But in that approach you can't dynamically add more data for the array, right? Or even dynamically increase the size the div "list"

    – Myntekt
    Nov 22 '18 at 22:29











  • You sure can. Computed properties react to changes in the underlying data (ie this.bigArrayOfData)

    – Phil
    Nov 22 '18 at 22:35











  • @Myntekt I've updated the answer to demonstrate Phil's comment.

    – DigitalDrifter
    Nov 23 '18 at 2:57














1












1








1







I would use reduce to create a "bucket" object keyed by the grouping indexes.




The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.




Example






new Vue({
el: '#app',
data() {
return {
dataArray:
}
},
computed: {
bucket () {
return this.dataArray.reduce((obj, item) => {
if (!obj.hasOwnProperty(item.index)) {
Object.assign(obj, {
[item.index]: [item]
})
} else {
obj[item.index].push(item)
}
return obj
}, {})
}
},
created() {
for (i = 0; i < 10; i++) {
this.addData()
}
},
methods: {
addData() {
this.dataArray.push({
index: Math.ceil(Math.random() * 3),
name: Math.random().toString(36).substring(7)
})
},
removeData() {
this.dataArray = this.dataArray.slice(1)
}
}
})

<div id="app">
<button @click="addData(true)">Add Data</button>
<button @click="removeData">Remove Data</button>
<div v-for="key in Object.keys(bucket)" :key="key">
<h3 style="font-family: monospace">Bucket {{ key }} ({{ bucket[key].length }} items)</h3>
<p v-for="(item, index) in bucket[key]" :key="item.name" style="font-family: monospace; border-bottom: 1px solid rgba(96,125,139,.1)">{{ index + 1 }}. {{ item.name }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.17/browser.min.js"></script></script>








share|improve this answer















I would use reduce to create a "bucket" object keyed by the grouping indexes.




The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.




Example






new Vue({
el: '#app',
data() {
return {
dataArray:
}
},
computed: {
bucket () {
return this.dataArray.reduce((obj, item) => {
if (!obj.hasOwnProperty(item.index)) {
Object.assign(obj, {
[item.index]: [item]
})
} else {
obj[item.index].push(item)
}
return obj
}, {})
}
},
created() {
for (i = 0; i < 10; i++) {
this.addData()
}
},
methods: {
addData() {
this.dataArray.push({
index: Math.ceil(Math.random() * 3),
name: Math.random().toString(36).substring(7)
})
},
removeData() {
this.dataArray = this.dataArray.slice(1)
}
}
})

<div id="app">
<button @click="addData(true)">Add Data</button>
<button @click="removeData">Remove Data</button>
<div v-for="key in Object.keys(bucket)" :key="key">
<h3 style="font-family: monospace">Bucket {{ key }} ({{ bucket[key].length }} items)</h3>
<p v-for="(item, index) in bucket[key]" :key="item.name" style="font-family: monospace; border-bottom: 1px solid rgba(96,125,139,.1)">{{ index + 1 }}. {{ item.name }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.17/browser.min.js"></script></script>








new Vue({
el: '#app',
data() {
return {
dataArray:
}
},
computed: {
bucket () {
return this.dataArray.reduce((obj, item) => {
if (!obj.hasOwnProperty(item.index)) {
Object.assign(obj, {
[item.index]: [item]
})
} else {
obj[item.index].push(item)
}
return obj
}, {})
}
},
created() {
for (i = 0; i < 10; i++) {
this.addData()
}
},
methods: {
addData() {
this.dataArray.push({
index: Math.ceil(Math.random() * 3),
name: Math.random().toString(36).substring(7)
})
},
removeData() {
this.dataArray = this.dataArray.slice(1)
}
}
})

<div id="app">
<button @click="addData(true)">Add Data</button>
<button @click="removeData">Remove Data</button>
<div v-for="key in Object.keys(bucket)" :key="key">
<h3 style="font-family: monospace">Bucket {{ key }} ({{ bucket[key].length }} items)</h3>
<p v-for="(item, index) in bucket[key]" :key="item.name" style="font-family: monospace; border-bottom: 1px solid rgba(96,125,139,.1)">{{ index + 1 }}. {{ item.name }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.17/browser.min.js"></script></script>





new Vue({
el: '#app',
data() {
return {
dataArray:
}
},
computed: {
bucket () {
return this.dataArray.reduce((obj, item) => {
if (!obj.hasOwnProperty(item.index)) {
Object.assign(obj, {
[item.index]: [item]
})
} else {
obj[item.index].push(item)
}
return obj
}, {})
}
},
created() {
for (i = 0; i < 10; i++) {
this.addData()
}
},
methods: {
addData() {
this.dataArray.push({
index: Math.ceil(Math.random() * 3),
name: Math.random().toString(36).substring(7)
})
},
removeData() {
this.dataArray = this.dataArray.slice(1)
}
}
})

<div id="app">
<button @click="addData(true)">Add Data</button>
<button @click="removeData">Remove Data</button>
<div v-for="key in Object.keys(bucket)" :key="key">
<h3 style="font-family: monospace">Bucket {{ key }} ({{ bucket[key].length }} items)</h3>
<p v-for="(item, index) in bucket[key]" :key="item.name" style="font-family: monospace; border-bottom: 1px solid rgba(96,125,139,.1)">{{ index + 1 }}. {{ item.name }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-template-compiler@2.5.17/browser.min.js"></script></script>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 3:08

























answered Nov 22 '18 at 22:25









DigitalDrifterDigitalDrifter

8,3382624




8,3382624













  • Well I have no idea what that does and how it works. But in that approach you can't dynamically add more data for the array, right? Or even dynamically increase the size the div "list"

    – Myntekt
    Nov 22 '18 at 22:29











  • You sure can. Computed properties react to changes in the underlying data (ie this.bigArrayOfData)

    – Phil
    Nov 22 '18 at 22:35











  • @Myntekt I've updated the answer to demonstrate Phil's comment.

    – DigitalDrifter
    Nov 23 '18 at 2:57



















  • Well I have no idea what that does and how it works. But in that approach you can't dynamically add more data for the array, right? Or even dynamically increase the size the div "list"

    – Myntekt
    Nov 22 '18 at 22:29











  • You sure can. Computed properties react to changes in the underlying data (ie this.bigArrayOfData)

    – Phil
    Nov 22 '18 at 22:35











  • @Myntekt I've updated the answer to demonstrate Phil's comment.

    – DigitalDrifter
    Nov 23 '18 at 2:57

















Well I have no idea what that does and how it works. But in that approach you can't dynamically add more data for the array, right? Or even dynamically increase the size the div "list"

– Myntekt
Nov 22 '18 at 22:29





Well I have no idea what that does and how it works. But in that approach you can't dynamically add more data for the array, right? Or even dynamically increase the size the div "list"

– Myntekt
Nov 22 '18 at 22:29













You sure can. Computed properties react to changes in the underlying data (ie this.bigArrayOfData)

– Phil
Nov 22 '18 at 22:35





You sure can. Computed properties react to changes in the underlying data (ie this.bigArrayOfData)

– Phil
Nov 22 '18 at 22:35













@Myntekt I've updated the answer to demonstrate Phil's comment.

– DigitalDrifter
Nov 23 '18 at 2:57





@Myntekt I've updated the answer to demonstrate Phil's comment.

– DigitalDrifter
Nov 23 '18 at 2:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438552%2fplace-things-in-a-specific-iteration-element-in-vue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen