javascript – So fügen Sie einem Objekt dynamische Eigenschaften hinzu
仅有的幸福
仅有的幸福 2017-07-05 10:53:58
0
2
576
var options = {pageName: 'index', position: 1800}
var obj = {}
现在要把这个属性添加到obj中变成
obj = {
  index:{position: options.position}
}
前面的index根据上面的pageName动态变化如何处理?
仅有的幸福
仅有的幸福

Antworte allen(2)
曾经蜡笔没有小新
var options = {pageName: 'index', position: 1800}
var obj = {} 
obj = {
  [options.pageName]:{position: options.position}
}
大家讲道理
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");
//这样?
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage