gpt4 book ai didi

JavaScript:将字符串 push() 到数组中返回 **null**

转载 作者:行者123 更新时间:2023-11-30 20:46:15 24 4
gpt4 key购买 nike

考虑以下输入:

{
"algorithm_run": "mean: 07/02/2018 02:38:09F1 (Mhz) mean",
"id": 12,
"parameter": "F1 (Mhz) mean",
"runs": "2017-09-19 15:41:00(0:00:59.350000)",
"start_time": "2017-09-19 15:41:00",
"value": 13.5629839539749
}

在包含该 JSON 的 URL 上调用以下函数:

function getScatterPlotRunData(url_string) {
$.getJSON(url_string, function(data) {
var graph_data = [];
for (i = 0; i < data.length; i++) {
graph_data.push({
"x" : i,
"y" : data[i].value,
"z" : data[i].start_time
});
};
drawScatterPlot(graph_data, data[0].parameter);
});
}

我们的想法是构建一个数组:

{
x: index of loop,
y: value from data[i].value,
z: time stamp string from data[i].start_time
}

我当前的输出将时间戳返回为 null:

{
x: 0
y: 13.5629839539749
z: null
}

我已经尝试了很多东西。转向 start_time.toString()(但它已经是一个字符串!),在单独的环境中调用该函数,重写整个过程。

我完全不确定我在这里做错了什么。

如果有任何帮助,我将不胜感激。这可能是由于我作为一名初级开发人员还不知道的数据类型怪癖。

提前感谢大家。

[编辑] 回答评论中的一些问题:

输入的 JSON 有效。事实上,帖子顶部的输入是从控制台复制的。我刚刚删除了网址。

在几个阶段记录到控制台显示以下内容:

console.log(data) 在定义函数之后:

"$.getJSON(url_string, function(data) {
console.log(data);
... // Rest of code
// Reveals entire valid JSON, with all fields as they should be. EG:
>>> start_time": "2017-09-19 15:41:00" // not NULL

console.log(data) 在 for 循环之后和循环内部:

for (i = 0; i < data.length; i++) {
console.log(data);
...
// Reveals entire JSON, with all fields as they should be. EG:
>>> start_time": "2017-09-19 15:41:00" // not NULL

console.log(typeof data);
>>> object // Still valid JSON

console.log(typeof graph_data);
>>> object // Array is type object.

console.log(graph_data);
>>> [] // Array is empty.

console.log(graph_data) 在调用 graph_data.push() 之后:

graph_data.push({
"x" : i,
"y" : data[i].value,
"z" : data[i].start_time
});
console.log(graph_data);
>>>0: {x: 0, y: 13.5629839539749, z: null}
// All values for time stamp are now null.

经过刚才的进一步测试:

graph_data.push({
"x" : i,
"y" : data[i].value,
"z" : i
});
console.log(graph_data);
>>>z: 1
// I can insert as many Key : Value pairs as I want
// As long as values are integers.

插入其他值:

graph_data.push({
"x" : i,
"y" : data[i].value,
"z" : "Foo"
});
console.log(graph_data);
>>>z: null
// Hard coding a "string" gives me a null value.

问题在于尝试 .push() 一个字符串作为值。我可以插入任何字符串作为键,但它不接受字符串作为值。

关于这是为什么的任何想法?

最佳答案

您的代码有点复杂,首先,您可以改用以下代码吗:

function convertCollectionElement(input, index){
return {
"x": index,
"y": input.value,
"z": input.start_time
};
}

function convertCollection(inputs){
return inputs.map(convertCollectionElement);
}

function checkForNullity(input, index){
if(input.z === null){
throw new Error("z was null on : "+JSON.stringify(input));
}
}

function getScatterPlotRunData(url_string) {
$.getJSON(url_string, function(data) {
var graph_data = convertCollection(data);

//result of conversion
graph_data.forEach(checkForNullity);

drawScatterPlot(graph_data, data[0].parameter);

//verify that "drawScatterPlot" didn't mofidy our data
graph_data.forEach(checkForNullity);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我相信这足以回答您的问题并解开这个谜团:)

祝你好运!

关于JavaScript:将字符串 push() 到数组中返回 **null**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48667023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com