You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除JSON对象中字段值的单引号?

Solution to Convert String Property Values to Direct Object References

Got it, let's break this down. You've got an object where some property values are strings like 'o.chat1', and you want to replace those strings with direct references to the actual properties on the o object. Here are a couple of ways to do this depending on your use case:

Option 1: Manual Assignment (Best for Fixed Fields)

If you know the exact fields you need to update (like chat1 and chat2 here), the simplest approach is to just build the new object directly. This is straightforward and avoids any dynamic parsing overhead:

// Your original data
var Data = { ques: [], chat1: 'o.chat1', chat2: 'o.chat2' };

// Assuming the `o` object is already defined in your scope
var newData = {
  ques: Data.ques, // Keep the original array reference
  chat1: o.chat1,
  chat2: o.chat2
};

Option 2: Dynamic Conversion (For Variable/Unknown Fields)

If you might have more fields like chat3, chat4, etc., or if you don't want to hardcode each property, you can loop through the original object and parse the string values to reference the o object:

var Data = { ques: [], chat1: 'o.chat1', chat2: 'o.chat2' };
var newData = { ques: Data.ques }; // Start with the unchanged `ques` array

// Map variable names to their actual values (safer than accessing globals directly)
const availableVars = { o };

for (const key in Data) {
  if (key !== 'ques') {
    // Split the string into object name and property name
    const [objName, propName] = Data[key].split('.');
    // Assign the actual property value to newData
    newData[key] = availableVars[objName][propName];
  }
}

Quick Note:

If o is a global variable (attached to window in browsers), you could swap availableVars[objName] with window[objName], but using an explicit mapping like availableVars is better—it prevents accidental access to unintended global variables.

After running either approach, newData will match your desired structure:

{ ques: [], chat1: o.chat1, chat2: o.chat2 }

内容的提问来源于stack exchange,提问作者ggegg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:10:42