Flex key-value存储

Flex中使用Object实现key-value存储
本文介绍如何在Flex项目中使用Object实现类似Java中Map的key-value存储方式,包括添加、删除、查找和遍历等操作。
Flex .key-value





项目需要通过设备ID来存储当前设备的可用Ip地址,如果放在Array里面,每次去除deviceID对应的Ip地址需要遍历。所以想利用类似于Java中Map这种key-value的形式,对数据进行操作。

果然,答案是Object类似于Java中的HashMap。

-------------

Demo1:添加数据



var temp:Object = new Object();



temp["ThinkPad"] = "10.1.1.1";

temp["LePad"] = "10.1.1.3";

temp["lePhone"] = "10.1.1.5";



-------------

Demo2:根据key删除当前记录

delete temp["LePad"];



-------------

Demo3: 根据Key获取对应的记录

var tempIp:String = temp["LePhone"];



-------------

Demo4: 关于Object的遍历---获取key



for( var i:String in temp){

Alert.show( "key =" + i); //获取key值

Alert.show( "key =" + temp[i]); //获取value

}



-------------

Demo5:关于Object的遍历----获取value



for each( var ip:String in temp){

Alert.show( "value =" + ip);

}
以下代碼中dntag欄位中顯示的每筆數組資料間以大寫分號隔開,垂直排列改成水平排列,每筆數組資料中的id:及dnname:都不顯示: <template> <div class="container"> <h1>合并后的数据</h1> <!-- 控制面板 --> <div class="control-panel"> <button @click="fetchData" class="refresh-btn">刷新数据</button> <input v-model="searchQuery" placeholder="搜索..." class="search-input" /> <div class="pagination-controls"> <span>每页显示:</span> <select v-model="pageSize" class="page-size-select"> <option value="1">1条</option> <option value="4">4条</option> <option value="10">10条</option> </select> <button @click="prevPage" :disabled="currentPage === 1">上一页</button> <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span> <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button> </div> </div> <!-- 垂直堆叠的数据展示 --> <div class="vertical-records" v-if="filteredData.length > 0"> <div v-for="(item, index) in paginatedData" :key="index" class="record-card"> <div class="record-header"> <h3>记录 #{{ (currentPage - 1) * pageSize + index + 1 }}</h3> </div> <div class="record-body"> <div v-for="(value, key) in item" :key="key" class="record-field"> <div class="field-name">{{ key }}:</div> <div class="field-value"> <div v-if="Array.isArray(value)" class="array-value"> <div v-for="(subItem, subIndex) in value" :key="subIndex" class="sub-item"> <div v-if="Array.isArray(subItem)" class="sub-array"> <div v-for="(val, i) in subItem" :key="i" class="key-value-pair"> <span class="key">{{ val[0] }}:</span> <span class="value" v-html="formatValue(val[1])"></span> </div> </div> <span v-else v-html="formatValue(subItem)"></span> </div> </div> <div v-else v-html="formatValue(value)"></div> </div> </div> </div> </div> </div> <div v-else class="no-data"> 没有找到匹配的数据 </div> </div> </template> <script> export default { data() { return { api1Data: [], api2Data: [], mergedData: [], currentPage: 1, pageSize: 1, searchQuery: '', sortKey: '', sortOrders: {} }; }, computed: { filteredData() { const query = this.searchQuery.toLowerCase(); return this.mergedData.filter(item => { return Object.values(item).some(value => { if (value === null || value === undefined) return false; const strValue = JSON.stringify(value).toLowerCase(); return strValue.includes(query); }); }); }, sortedData() { if (!this.sortKey) return this.filteredData; const order = this.sortOrders[this.sortKey] || 1; return [...this.filteredData].sort((a, b) => { const getValue = (obj) => { const val = obj[this.sortKey]; if (Array.isArray(val)) { return JSON.stringify(val); } return val; }; const aValue = getValue(a); const bValue = getValue(b); if (aValue === bValue) return 0; return aValue > bValue ? order : -order; }); }, paginatedData() { const start = (this.currentPage - 1) * this.pageSize; const end = start + this.pageSize; return this.sortedData.slice(start, end); }, totalPages() { return Math.ceil(this.filteredData.length / this.pageSize) || 1; } }, methods: { async fetchData() { try { const api1Response = await fetch("MRInfo/?format=json"); this.api1Data = await api1Response.json(); const api2Response = await fetch("DNTag/?format=json"); this.api2Data = await api2Response.json(); this.mergeData(); this.currentPage = 1; } catch (error) { console.error("获取数据失败:", error); } }, mergeData() { this.mergedData = this.api1Data.map((item) => { const newItem = { ...item }; if (newItem.dntag && Array.isArray(newItem.dntag)) { newItem.dntag = newItem.dntag.map((tagId) => { const matchedItem = this.api2Data.find( (api2Item) => api2Item.id === tagId ); const tagObj = matchedItem || { id: tagId, name: "未找到匹配数据" }; return Object.entries(tagObj).map(([key, value]) => [key, value]); }); } return newItem; }); this.sortOrders = {}; if (this.mergedData.length > 0) { Object.keys(this.mergedData[0]).forEach(key => { this.sortOrders[key] = 1; }); } }, formatValue(value) { if (value === null || value === undefined) return ''; if (typeof value === 'string' && value.startsWith('http')) { return `<a href="${value}" target="_blank">${value}</a>`; } return value; }, sortBy(key) { this.sortKey = key; this.sortOrders[key] = this.sortOrders[key] * -1; }, prevPage() { if (this.currentPage > 1) { this.currentPage--; } }, nextPage() { if (this.currentPage < this.totalPages) { this.currentPage++; } } }, mounted() { this.fetchData(); } }; </script> <style scoped> .container { max-width: 1200px; margin: 0 auto; padding: 20px; } .control-panel { margin-bottom: 20px; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; } .refresh-btn { padding: 8px 16px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; } .refresh-btn:hover { background-color: #45a049; } .search-input { padding: 8px; border: 1px solid #ddd; border-radius: 4px; flex-grow: 1; max-width: 300px; } .pagination-controls { display: flex; align-items: center; gap: 10px; } .page-size-select { padding: 8px; border-radius: 4px; } /* 垂直记录样式 */ .vertical-records { display: flex; flex-direction: column; gap: 20px; } .record-card { border: 1px solid #ddd; border-radius: 4px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .record-header { padding: 12px 16px; background-color: #f5f5f5; border-bottom: 1px solid #ddd; } .record-header h3 { margin: 0; font-size: 1.1em; } .record-body { padding: 16px; } .record-field { display: flex; margin-bottom: 12px; line-height: 1.5; } .record-field:last-child { margin-bottom: 0; } .field-name { font-weight: bold; min-width: 120px; color: #555; } .field-value { flex-grow: 1; } .array-value { display: flex; flex-direction: column; gap: 8px; } .sub-item { margin-bottom: 4px; } .sub-array { display: flex; flex-direction: column; gap: 4px; margin-top: 4px; } .key-value-pair { display: flex; gap: 8px; } .key { font-weight: 500; color: #666; } .value { word-break: break-word; } .no-data { padding: 20px; text-align: center; color: #666; font-style: italic; } button:disabled { opacity: 0.5; cursor: not-allowed; } </style>
07-06
<template> <view> <view class="w-94" style="margin-top: 32rpx;"> <view class="text-lg text-bold" style="color: #1D2129;"> 求购内容 </view> <view class="" style="margin-top: 32rpx;"> <view class="container"> <view class=""> <u-input v-model="inputValue" :custom-style="inputCustomStyle" type="textarea" placeholder="请输入内容" maxlength="400" height="224" @input="onInput"></u-input> </view> <view class="counter"> {{ currentLength }}/200 </view> </view> </view> <view class="w-94 text-lg text-bold" style="color: #1D2129; margin-top: 32rpx; margin-bottom: 8rpx;"> 车源信息 </view> <view class="flex align-center justify-between padding-lr" style="height: 108rpx; border-bottom: 1rpx solid #E5E6EB;"> <view class="flex align-center justify-between"> <view class="text-df text-bold" style="color: #1D2129;"> 品牌车型 </view> <view class="" style="color: #C9CDD4; margin-left:64rpx"> 请选择 </view> </view> <view class=""> <u-icon name="arrow-right" color="#86909C"></u-icon> </view> </view> <view class="w-94 text-df text-bold" style="color: #1D2129; margin-top: 32rpx; margin-bottom: 8rpx;"> 车辆图片 </view> <view class=""> <view class="wrap flex align-center justify-start"> <view class="pre-box" v-if="!showUploadList"> <view class="pre-item" v-for="(item, index) in lists" :key="index"> <image class="pre-item-image" :src="item.url" mode="aspectFill"></image> </view> </view> <u-upload :custom-btn="true" ref="uUpload" :show-upload-list="showUploadList" :action="action"> <view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150"> <u-icon name="plus" size="60" color="#c0c4cc"></u-icon> </view> </u-upload> </view> </view> <view class="flex align-center justify-between padding-lr" @click="clicShow()" style="height: 108rpx; border-bottom: 1rpx solid #E5E6EB;"> <view class="flex align-center justify-between"> <view class="text-df text-bold" style="color: #1D2129;"> 谁可以见 </view> <view class="" style="color: #4E5969; margin-left:64rpx"> 所有人可见 </view> </view> <view class=""> <u-icon name="arrow-right" color="#86909C"></u-icon> </view> </view> <view class="fixed-button safe-area-inset-bottom" @click="submit()"> <view class="flex align-center justify-center" style="background-color: #13D28D; height: 88rpx; border-radius: 16rpx;"> <view class="text-bold text-lg" style="color: #fff;"> 发布 </view> </view> </view> </view> <u-popup v-model="show" mode="bottom" height="460rpx" border-radius="24"> <view class="flex align-center justify-center text-bold text-lg " style="height: 109rpx;border-bottom:2rpx solid #E5E6EB;"> 所有人可见 </view> <view class="flex align-center justify-center text-lg text-bold" style="height: 109rpx;"> 仅粉丝可见 </view> <view class="flex align-center justify-center text-lg text-bold" style="height: 109rpx;"> 仅自己可见 </view> <view class="flex align-center justify-center" style="height: 16rpx; background-color: #E5E6EB;"> </view> <view class="flex align-center justify-center text-lg" style="height: 109rpx;"> 取消 </view> </u-popup> </view> </template> <script> export default { data() { return { show: false, currentLength: 0, action: '/upload', // 演示地址 showUploadList: false, lists: [], inputValue: '', inputCustomStyle: { borderRadius: '16rpx' } }; }, methods: { clicShow() { this.show = true }, onInput(value) { this.currentLength = value.length } }, onReady() { // 得到整个组件对象,内部图片列表变量为"lists" this.lists = this.$refs.uUpload.lists; } } </script> <style lang="scss"> .container { position: relative; background-color: #F7F8FA; border-radius: 16rpx; // padding: 20rpx; // margin: 20rpx; } /* 计数器样式 */ .counter { position: absolute; right: 40rpx; bottom: 20rpx; font-size: 24rpx; color: #999; background-color: #F7F8FA; padding: 0 10rpx; } .wrap { padding: 24rpx; } .slot-btn { width: 224rpx; height: 224rpx; display: flex; justify-content: center; align-items: center; background: rgb(244, 245, 246); border-radius: 10rpx; } .slot-btn__hover { background-color: rgb(235, 236, 238); } .pre-box { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; } .pre-item { flex: 0 0 48.5%; border-radius: 10rpx; height: 140rpx; overflow: hidden; position: relative; margin-bottom: 20rpx; } .pre-item-image { width: 100%; height: 140rpx; } .fixed-button { position: fixed; left: 0; right: 0; bottom: 0; /* 关键:贴住底部 */ z-index: 1000; padding: 50rpx; box-sizing: border-box; } </style> 我想要弹窗选择的值放入输入框内
最新发布
12-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值