将对象转换为对象对象

时间:2018-01-10 15:48:56

标签: javascript jquery object

我有一个对象:

{
"Results": {
    "circle": 0.06879016757011414,
    "quad": {
        "exp": 0.8039023876190186,
        "actual": 0.19609761238098145
    },
    "square": 0.8266428709030151
 }
}

我想将其转换为:

{
"Results": {
    "circle": {
        "circle": 0.06879016757011414
    },
    "quad": {
        "exp": 0.8039023876190186,
        "actual": 0.19609761238098145
    },
    "square": {
        "square": 0.8266428709030151
    }
  }
}

试过这段代码:

var modData = {};
data = data.Results;
for (var key in data) {
  if (data.hasOwnProperty(key)) {
    modData[key] = data[key];
    for (var innerKey in data[key]) {
      modData[key] = data[key];
    }
  }
}
console.log("Modified is:", modData);

不幸的是,这仍然会返回原始对象,我正在做什么是错误的?

jquery解决方案也很好。

4 个答案:

答案 0 :(得分:1)

使用for .. in循环使用属性,并且任何属性都不是对象,请将其替换为一个。像这样:

let x = {
  "Results": {
    "circle": 0.06879016757011414,
    "quad": {
      "exp": 0.8039023876190186,
      "actual": 0.19609761238098145
    },
    "square": 0.8266428709030151
  }
}
for (key in x.Results) {
  if (typeof x.Results[key] !== 'object')
    x.Results[key] = {
      [key]: x.Results[key]
    }
}

console.log(x);

如果要保留原始对象,请执行以下操作:

let data = {
  "Results": {
    "circle": 0.06879016757011414,
    "quad": {
      "exp": 0.8039023876190186,
      "actual": 0.19609761238098145
    },
    "square": 0.8266428709030151
  }
}

data = data.Results;
modData = {};

for (key in data) {
  if (typeof data[key] !== 'object')
    modData[key] = { [key]: data[key] }
  else
    modData[key] = { [key]: data[key] }
}

console.log(modData);

答案 1 :(得分:0)

您可以检查类型,如果不是对象,则分配具有相同键和值的新对象。

此提案使用computed property names将变量作为对象的键。

.as-console-wrapper { max-height: 100% !important; top: 0; }
{{1}}

答案 2 :(得分:0)

应该存储在modData中的值应该是对象本身而不是值。此代码给出了期望值

    StimulusSecs = 0.50001;
    StimDuration = round(StimulusSecs / ifi);
    ISISecs = 0.50001;
    ISI = round(ISISecs / ifi);
    ITISecs = [1.00001, 2.00001, 3.00001];
    intertrial = round(ITISecs / ifi);
    waitframes = 1;
    vbl = Screen('Flip', mainwin);
    t2wait = 1;

    [~, Onset]=Screen('Flip', mainwin); keyIsDown=0; rt=0;

    tic % Stimulus Onset

    for frame = 1:StimDuration
    Screen('DrawTexture', mainwin, pics(picCounter));
    vbl = Screen('Flip',mainwin, vbl + (waitframes - 0.5) * ifi);
    [keyIsDown, secs, keyCode] = KbCheck;
    FlushEvents('keyDown');
    if keyIsDown
        if keyCode(spaceKey)
            rt = 1000.*(secs-Onset);
            keypressed=find(keyCode); 
            % break; is missing as this would 
            % escape the loop
        elseif (secs-Onset) > t2wait
            break;
        elseif keyCode(escKey)
            ShowCursor; fclose(outfile); Screen('CloseAll');
            return;
        end
        keyIsDown=0; keyCode=0; keypressed=0;
    end
end
S2dur = toc;

答案 3 :(得分:0)

    const input = {
        "Results": {
            "circle": 0.06879016757011414,
            "quad": {
                "exp": 0.8039023876190186,
                "actual": 0.19609761238098145
            },
            "square": 0.8266428709030151
        }
    };


    const result = {
        Results: {}
    };

    //If you want object construct specifically for "circle" & "square"
    for (let key in input.Results) {
        if (input.Results.hasOwnProperty(key)) {
            if (key === 'circle') {
                result.Results.circle  = {
                    'circle': input.Results[key]
                }
            } else if (key === 'square') {
                result.Results.square = {
                    'square': input.Results[key]
                }
            } else {
                result.Results[key] = input.Results[key];
            }
        }
    }

//Generically construct object if key is not object
for (let key in input.Results) {
    if (input.Results.hasOwnProperty(key)) {
        if (key !== 'object') {
            result.Results[key] = {
                [key]: input.Results[key]
            }
        }
    }
}


    console.log(result);