简体   繁体   English

如何在 Axios 响应上将 JSON 数组正确推送到 Vue JS 中的现有数据数组中?

[英]How do I properly push JSON array onto existing data array in Vue JS on a Axios response?

I think this is a general Javascipt question however I am working in Vue Js, Laravel & Axios.我认为这是一个通用的 Javascipt 问题,但是我在 Vue Js、Laravel 和 Axios 工作。

How do I push one JSON array into another JSON array?如何将一个 JSON 数组推入另一个 JSON 数组? My problem is that when I push the secondary array into the primary array it is nested (see screenshot).我的问题是,当我将辅助数组推入主数组时,它是嵌套的(见截图)。 I need it as part of the same array.我需要它作为同一个数组的一部分。

Is this a simple index issue?这是一个简单的索引问题吗? I've read concat can achieve this, but I have a "load more" button and wish to append the array and increase the size from the bottom, so push is appropriate.我读过concat可以实现这一点,但我有一个“加载更多”按钮并希望附加数组并从底部增加大小,所以push是合适的。 concat creates a new array and replaces current, which isn't desired as I want to maintain the existing array and just increase size for smoother loading of results, like on Pinterest scroll and the new results populated at the bottom. concat 创建一个新数组并替换当前数组,这是不希望的,因为我想维护现有数组并只是增加大小以更平滑地加载结果,例如在 Pinterest 滚动和底部填充的新结果。

(this is simplified code to get point across) (这是简化的代码,可以理解)

Data property empty array to start:数据属性空数组开始:

    data () {
        return {
            articles: [],
        }
    },

Firstly, on page load the articles array is populated with JSON data.首先,在页面加载时,articles 数组会填充 JSON 数据。

created() {
    axios.get(url)
        .then(response => {
            this.articles = response.data.data;
    });
},

Then, I want to 'load more' results via method然后,我想通过方法“加载更多”结果

loadMore() {
    axios.get(url)
        .then(response => {

            console.log('article\'s array data')
            console.log(this.articles)

            this.articles.push(response.data.data)

            console.log('new response array data')
            console.log(response.data.data)

            console.log('the updated array with pushed response array')
            console.log(this.articles)

    });
}

console log控制台日志

在此处输入图像描述

This should be an array of 5 + 5 = 10 for length of articles array if properly appended.如果正确附加,这应该是一个 5 + 5 = 10 的数组,用于文章数组的长度。

both responses are correct and matching json as I've got it working via concat to merge.两个响应都是正确的并且匹配 json,因为我已经通过 concat 进行合并。 But undesirable as it reloads entire array.但不可取,因为它会重新加载整个数组。

If you are against .concat, which is super a very straightforward and performant way, you may try the following:如果您反对 .concat,这是一种非常简单且高效的方式,您可以尝试以下方法:

const myArray = [1,3,4,5];
myArray.push(...[6,7,8,9);
console.log(myArray); // this will contain [1,2,3,4,5,6,7,8,9] now

Taken from advice from both @noa-dev and @Shilly above - I used concat as below.从上面的@noa-dev 和@Shilly 的建议中获取 - 我使用 concat 如下。 Response had to be set in a var for promise as didn't work without.必须在 var 中为 promise 设置响应,因为没有它就无法工作。 Thanks.谢谢。

var new_array = response.data.data
this.articles = this.articles.concat(new_array)

Explanation how Vue doesn't actually replace the array when using concat!解释 Vue 在使用 concat 时实际上并没有替换数组! :) https://v2.vuejs.org/v2/guide/list.html#Replacing-an-Array :) https://v2.vuejs.org/v2/guide/list.html#Replacing-an-Array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM