数组的操作,处理多个条件的值判断和累加,修改属性

```

const vm = new Vue({

            el: "#root",

            data() {

                return {

                    list: [{

                            id: 1,

                            name: 'air',

                            age: 23,

                            height: 23

                        },

                        {

                            id: 2,

                            name: 'ar',

                            age: 26,

                            height: 30

                        },

                        {

                            id: 3,

                            name: 'ai',

                            age: 27,

                            height: 43

                        }

                    ],

                    list2: [{

                            id: 1,

                            name: 'air',

                            age: 23,

                            height: 23

                        },

                        {

                            id: 2,

                            name: 'ar',

                            age: 26,

                            height: 30

                        },

                        {

                            id: 3,

                            name: 'ai',

                            age: 27,

                            height: 43

                        }

                    ],

                    obj: {

                        id: 0,

                        name: '',

                        age: 0,

                        height: 0

                    },

                    ss: [],

                    rowIndex: 0

                }

            },

            created() {

                this.getData();

                // 过滤后累加的数据,伪数组形式

                this.handle(this.list);

                // 处理操作,filter和map都会改变原数组,这里是map

                this.changeData(this.list);

                // 赋值操作

                this.ss = this.list;

                // 处理操作,filter和map都会改变原数组,同上,这里是filter

                this.inCreace(this.list2);

            },

            methods: {

                // 编辑数组对象,传递一行数据过来

                editUser(item, index) {

                    this.rowIndex = index;

                    this.obj = {

                        id: item.id,

                        name: item.name,

                        age: item.age,

                        height: item.height

                    }

                },

                // 累加处理

                handle(array) {

                    return array.map(item => {

                        const obj = {};

                        obj.name = item.name;

                        obj.children = [

                            item.age > 23 ? item.age : 0,

                            item.height > 30 ? item.height : 0

                        ]

                        obj.total = obj.children.reduce((pre,cur) => pre+cur);

                        return obj;

                    });

                    console.log(this.ss,'ssb');

                },

                // map遍历数组返回修改的对象key值名字

                changeData(array) {

                    return array.map(item => {

                        item.name == 'air' ? item.name = 'AIR': item

                    });

                },

                // filter遍历数组返回修改的对象key值名字

                inCreace(array) {

                    return array.filter(item => {

                        return item.name == 'air' ? item.name = 'AIR': item

                    });

                }

            }

        })

```

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容