How object data is converted into an array


foreword

Sometimes the data given by the backend in the project cannot fully meet the data requirements of the frontend. For example, in the element-ui+vue project portfolio, the data in the drop-down box is in the form of an array. However, the back-end colleagues are indeed object data. Of course, you can negotiate with the back-end to change it to the data you want... But this is often troublesome. The front-end who likes to write code quietly decides to convert it by itself.


提示:正文内容

1. The data given by the backend

insert image description here

2. How to convert

			console.log('res:', res);
            let resObj = res.data.data; //获取后台给的对象数据
            let arr = []; //定义一个空数组
            for (let key in resObj) {
    
    
            //用for循环转换resObj对象
              arr.push({
    
    
                value: key,
                label: resObj[key]
              });
            }
            this.deviceList = arr; //将改数组赋值给数组deviceList,渲染到页面上
		  <el-form-item
              label="报警类型"
              prop="roomNumber"
            >
              <el-select
                placeholder="请选择"
                clearable
                default-first-option
                v-model="formInline.alarmDeviceType"
                @change="onSearch"
              >
                <el-option
                  v-for="(list,k) in deviceList"  //将数组渲染到页面上
                  :key="k"
                  :label="list.label"
                  :value="list.value"
                ></el-option>
              </el-select>
            </el-form-item>

3. Final result

insert image description here
That's all for sharing, if it helps you, don't forget to like it!

Guess you like

Origin blog.csdn.net/daishu_shu/article/details/124316621