-1

我想将 qm.negativesData 字符串添加到下面的 qm.data 中。但我得到一个语法错误。谁能帮我解决这个问题?

qm.data = [{
    "xScale":"ordinal",
    "comp":[],
    "main":[{qm.negativesData},{qm.positivesData}],
    "type":"line-dotted",
    "yScale":"linear"}];

qm.negativesData 和 qm.positivesData 包含一个字符串,其中包含用于构建我的图表的数据。

/// 更新

例如 qm.negativesData 的输出是:

"{"className":".main.l1","data":[{"x":"2013-10-16","y":"3283581"},{"x":"2013-10-17","y":"1512116"},{"x":"2013-10-18","y":"3967"},{"x":"2013-10-19","y":"1094"},{"x":"2013-10-20","y":"853"},{"x":"2013-10-21","y":"1205"},{"x":"2013-10-22","y":"2618700"},{"x":"2013-10-23","y":"3928291"},{"x":"2013-10-24","y":"3670318"},{"x":"2013-10-25","y":"3347369"},{"x":"2013-10-26","y":"2525573"},{"x":"2013-10-27","y":"3224612"},{"x":"2013-10-28","y":"3992964"},{"x":"2013-10-29","y":"3949904"},{"x":"2013-10-30","y":"3568618"},{"x":"2013-10-31","y":"3104696"},{"x":"2013-11-01","y":"3246932"},{"x":"2013-11-02","y":"2817758"},{"x":"2013-11-03","y":"3198856"},{"x":"2013-11-04","y":"3952957"},{"x":"2013-11-05","y":"3934173"},{"x":"2013-11-06","y":"3878718"},{"x":"2013-11-07","y":"3642822"},{"x":"2013-11-08","y":"3388646"},{"x":"2013-11-09","y":"318335"}]}"

我认为它不应该以“开头并以”结尾,这会导致错误。有没有办法排除“?

4

3 回答 3

2

在 JavaScript 对象字面量中,花括号用于键值对。因此,{qm.negativesData}无效。

如果要设置为包含andmain值的数组,可以使用:qm.negativesDataqm.positivesData

qm.data = [{
    "xScale":"ordinal",
    "comp":[],
    "main":[qm.negativesData, qm.positivesData],
    "type":"line-dotted",
    "yScale":"linear"}];
于 2013-11-08T21:51:30.570 回答
0

尝试使用一个简单的数组:

qm.data = [{
    "xScale":"ordinal",
    "comp":[],
    "main":[qm.negativesData,qm.positivesData],
    "type":"line-dotted",
    "yScale":"linear"}];

这将意味着qm.data.main[0] == qm.negativesDataqm.data.main[1] == qm.positivesData

或者你可能想要这样的东西:

qm.data = [{
    "xScale":"ordinal",
    "comp":[],
    "main":{
        "negativesData":qm.negativesData,
        "positivesData":qm.positivesData},
    "type":"line-dotted",
    "yScale":"linear"}];

这将意味着qm.data.main.negativesData == qm.negativesDataqm.data.main.positivesData == qm.positivesData

于 2013-11-08T21:50:55.223 回答
0

用于Array.prototype.concat将两个数组连接成一个新数组:

qm.data = [{
    "xScale":"ordinal",
    "comp":[],
    "main":qm.negativesData.concat(qm.positivesData),
    "type":"line-dotted",
    "yScale":"linear"}];

或用于+将两个字符串相加成一个新字符串:

qm.data = [{
    "xScale":"ordinal",
    "comp":[],
    "main":qm.negativesData + qm.positivesData,
    "type":"line-dotted",
    "yScale":"linear"}];

目前尚不清楚您拥有哪些数据以及(更重要的是)您最终想要哪些数据。main需求领域是什么qm.data样的?

PS 如果它们是有效的标识符,则不需要引用密钥的名称,这是 JavaScript 而不是 JSON。例如,只是qm.data = [{xScale:"ordinal", comp:[], …}];

于 2013-11-08T21:53:41.597 回答