Arrays Vuejs:如何创建观察项目列表的计算属性?

Arrays Vuejs:如何创建观察项目列表的计算属性?,arrays,vuejs2,computed-properties,Arrays,Vuejs2,Computed Properties,我想在我的应用程序中同步两个数组中的数据。我正在使用vue.js 我的第一个数组如下所示: var testLayout = [ {"x":0,"y":0,"w":2,"h":2,"i":"0"}, {"x":2,"y":0,"w":2,"h":4,"i":"1"} ]; var items = [ item { //other properties Position = {

我想在我的应用程序中同步两个数组中的数据。我正在使用vue.js

我的第一个数组如下所示:

    var testLayout = [
        {"x":0,"y":0,"w":2,"h":2,"i":"0"},
        {"x":2,"y":0,"w":2,"h":4,"i":"1"}
    ];
    var items = [
        item {
            //other properties
            Position = {
                x,
                y,
                Width,
                Height,
                MinWidth,
                MaxWidth,
                MinHeight,
                MaxHeight
            }
        },
        ....
    ]
第二个包含item对象的是:

    var testLayout = [
        {"x":0,"y":0,"w":2,"h":2,"i":"0"},
        {"x":2,"y":0,"w":2,"h":4,"i":"1"}
    ];
    var items = [
        item {
            //other properties
            Position = {
                x,
                y,
                Width,
                Height,
                MinWidth,
                MaxWidth,
                MinHeight,
                MaxHeight
            }
        },
        ....
    ]
我希望第一个数组中的属性与第二个数组中的属性发生变化,反之亦然。 我需要像这样同步我的数据:
testLayout[…].myAnonymousObject.x items[…].item.Position.x

如何创建计算属性或我可以用来实现这一点的东西?我无法更改这两个数组/对象的结构,但我需要随时更新它们的变化

我尝试在我的vue实例中执行此操作:

computed: {
            layout: {
                get: function () {
                    let allPositions = [];
                for (var item of items) {
                    allPositions.push(
                        {
                            x: item .Position.x,
                            y: item .Position.y,
                            h: item .Position.Height,
                            w: item .Position.Width,
                            i: item .Id
                            //do not set here min/maxW and min/maxH
                        }
                    );
                }
                return allPositions;
                },
                set: function () {
                    ???
                }
            }

        },

但这不起作用,我不知道如何将这些属性真正绑定在一起。你知道怎么做吗?我使用计算属性来做这件事是正确的吗

在我的实际项目中,我通过了类似的测试。 我已经搜索过了,有一个选项可以深入观察,是数组数组、对象数组、数组对象等的最佳选项

应该是这样的:

watch: {
     <variableName>: { // should be the name of the variable you want to watch
        // if you want, this handler() allows 2 params like (newValue, oldValue) to compare something if you want.
        handler() {
            // do your logic
            // call a function with the logic
        },
        deep: true
    }
}
观察:{
:{//应该是要监视的变量的名称
//如果需要,这个handler()允许两个参数(newValue,oldValue)进行比较。
handler(){
//做你的逻辑
//用逻辑调用函数
},
深:是的
}
}