Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

vue.js - How to merge (equal and unequal length) arrays by Id on Created() in Vue2

I need a code advice, since not finding a solution...Potentially two part question:

On Created(), I have 2 arrays that I want to combine (one of arrays is an external Axios API)

'''

<template>
      <div v-for="merged in mergedArray.slice(0, 5)">

          Title: {{merged.title}}
          <!-- this is initaiall from arrayAxios  -->

          Color: {{merged.color}}
          <!-- this is initaiall from arrayVue  -->

      </div>
</template>

<script>
import axios from "axios";

export default {
    data() {
        return {
            arrayAxios: null,
            arrayVue: [
                {id:1, name: "Paris", color: "blue"},
                {id:1, name: "Milano", color: "green"},
                {id:1, name: "London", color: "gray"}
            ],
            mergedArray:null 
        }
    },
    created(){
        axios.get('https://jsonplaceholder.typicode.com/albums/')
            .then((response) => {
                this.arrayAxios = response.data;
        });    
    },
}
</script>

'''

1st part (option): The IDs match, equal length arrays. On Created (or Mounted), the idea would be to push data from the Axios fetched array (arrayAxios in the example) into the already existing one (arrayVue in the example), and display the combined new result. The merging is to be done by matching the exact Ids, and thus just concatenating the existing properties of an element in the aaray, into a new array (mergedArray).

2nd part (less important for me, but interesting maybe for others): The Ids do not match fully, since unequal arrays Lengths of arrays are different (does not matter which one), thus only portion of the Ids match, so it would have to be a loop to return to the initial element once the shorter arrays gets exhausted (fully concatenated) and assigned new properties. For example, the Axios request (longer array) will get 100 response to only 3 arrayVue elements, so the first three Axios results would get assigned to the Ids 1, 2, 3 respectively of the arrayVue, and push into the mergedArray. However, the next 3 Axios will continue the loop and the results will again face the only three initial arrayVue elements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For your first case, you can make use of a filter() to check whether the id in the two arrays matches or not.

this.arrayAxios.filter(arr => this.arrayVue.filter(a => a['id'] === arr['id']))

This will return those objects from arrayAxios which matches the id of arrayVue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...