每日一条js之数组操作find()查询

今天在做项目的时候遇到了elementUI级联选择器选择地区的功能,地区的信息是百度复制过来的一段json数据
获取选中数据的时候只能获取到一个地区对应的value值,必须通过这个value值找到对应的label(的名字)。这次不再使用for循环改用较为简洁的find()函数直接return查询条件那条数据
change (a) {
    var that = this
    var obj = {};
    var obj1 = {};
    var obj2 = {};
    obj = that.options.find(function(item){
        return item.value == a[0]
    });
    that.province = obj.label
    that.city = null
    that.district = null
    if(a[1]){
        obj1 = obj.children.find(function (items) {
            return items.value == a[1]
        })
        that.city = obj1.label
    }
    if(a[2]){
        obj2 = obj1.children.find(function (i) {
            return i.value == a[2]
        })
        that.district = obj2.label
    }
},

猜你喜欢

转载自blog.csdn.net/qq_40816649/article/details/83904090