文章详情页
javascript - 如何给一个对象添加动态属性
问题描述
var options = {pageName: ’index’, position: 1800}var obj = {}现在要把这个属性添加到obj中变成obj = { index:{position: options.position}}前面的index根据上面的pageName动态变化如何处理?
问题解答
回答1:var options = {pageName: ’index’, position: 1800}var obj = {} obj = { [options.pageName]:{position: options.position}}回答2:
var result = {};function objSet(keyName,targetName){ var obj = {}; for(var key in options){if( key == keyName){ var temp = {}; temp[targetName] = options[targetName]; obj[keyName] = temp; break;} } return obj;}result = objSet('pageName','position');//这样?
标签:
JavaScript
相关文章:
排行榜