javascript - How to remove data with __ob__ in vue2?
给我你的怀抱
给我你的怀抱 2017-05-19 10:21:14
0
3
1732
data: function () {
    return {
        cityData: cityData,
        selectedOptions:this.source[this.schema_key]
    }
}

This.source[this.schema_key] on the console is as follows

[24, 81, __ob__: Observer]

Is there a way to directly obtain such data

[24, 81]
给我你的怀抱
给我你的怀抱

reply all(3)
PHPzhong

__ob__: ObserverThese data are monitors set by the vue framework for data, and are generally non-enumerable.

console.log这样的打印函数,被打印的变量会执行自身的toString() In this way, even if the internal properties are not enumerable, they can actually be seen. For example:

const obj = {
    a: 0,
    b: 1
};
Object.defineProperty(obj, 'b', {
    writable: false,
    enumerable: false,
    configurable: false
});

console.log(obj);    // Object {a: 0, b: 1}

Because you have bound the data in vue, vue must add monitors for the data. If you forcefully delete these monitors, then the data will lose monitoring, so what is the point of using vue? Where is...


If you just remove these monitors without considering the consequences, just copy the object, because the copied object does not contain non-enumerable attributes.

const obj1 = {
    a: 0,
    b: 1
};
Object.defineProperty(obj1, 'b', {
    writable: false,
    enumerable: false,
    configurable: false
});
const obj2 = Object.assign({}, obj1);

console.log(obj2);    // Object {a: 0}

In dynamic languages ​​like js, copying objects is a headache. I wrote a simple one, you can refer to it:

//对象深复制,不考虑循环引用的情况
function cloneObj(from) {
    return Object.keys(from)
        .reduce((obj, key) => (obj[key] = clone(from[key]), obj), {});
}
//数组深复制,不考虑循环引用的情况
function cloneArr(from) {
    return from.map((n) => clone(n));
}
// 复制输入值
function clone(from) {
    if (from instanceof Array) {
        return cloneArr(from);
    } else if (from instanceof Object) {
        return cloneObj(from);
    } else {
        return (from);
    }
}

const obj = [
    {
        name: '1'
    },
    {
        name: '2'
    }
];
const obj2 = clone(obj);
console.log(obj2);

Just use the clone() method directly outside.

小葫芦

This has no impact on the data~

曾经蜡笔没有小新

Similarly, solve it. . . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template