How does the vue object pass variables to get values

How does the vue object pass variables to get values

explain

In the process of development, when using keythe value, keyit does not have to be a fixed value, it may also be a variable.

The case code is as follows:

let key1 = 'name';

let objectList = {
    
    
  'id': '1',
  [key1]: '李四'
};

console.log(objectList['id']) // "1"
console.log(objectList[key1]) // "李四"
console.log(objectList.id) // '1'

Get the corresponding value method according to the key

let ticketLists: [{
    
    
	id: 1,
	name: '票证1',
	ticketone: '票证1图',
	checked: true,
},
{
    
    
	id: 2,
	tickettow: '票证2图',
	name: '票证2',
	checked: true
},
{
    
    
	id: 3,
	name: '票证3',
	ticketthree: '票证3图',
	checked: true
}]
getTicketImageName(objectList, keyName) {
    
    
	for(let item in objectList) {
    
    
		console.log("wdebug---key1",objectList[item][keyName]) // 使用变量的key取值
		console.log("wdebug---key2", objectList[item].ticketone) // 使用固定的key取值
		// 循环对象,取出对应的key
		for (let index in objectList[item]) {
    
    
			// 根据对象中的key和keyName 取出对应的value
			if(index == keyName) {
    
    
				// console.log('wdebug--key值', objectList[item][keyName])
				return objectList[item][keyName] // 返回对应的value
			}
		}
	}
}

// 使用方法 
created() {
    
    
	this.getTicketImageName(ticketLists, 'ticketone')
}

insert image description here

Guess you like

Origin blog.csdn.net/zxh7770/article/details/124405585