首页 > 解决方案 > 如何读取同一行中的下一个值?

问题描述

我想读取变量“x1”中的值“-2.5”,以及另一个名为“y1”的变量中的值“0.4”。下面的行也是如此:在变量“x2”中读取“12.1”并在变量“y2”中读取“7.3”

var lines = [
    "-2.5 0.4", 
    "12.1 7.3"
];
var x1 = parseFloat(lines[0]);
var y1 = jQuery(x1).next();
var x2 = parseFloat(lines[1]);
var y2 = jQuery(x2).next();

console.log(x1);
console.log(y1);
console.log(x2);
console.log(y2);

这是我正在解决的问题 和到目前为止我制作的代码,但不接受“错误答案 85%”

未捕获的 ReferenceError:未定义 jQuery

标签: javascript

解决方案


我认为您可以查看此解决方案以供参考,希望对您有所帮助

var lines = [
  "-2.5 0.4",
  "12.1 7.3"
];

// This will convert a string into an array seperated by space (" ")
const generatePosition = string => string.split(" ");

// This will map all our initial values and change them into a full array
const pos = lines.map(string => generatePosition(string));
console.log(pos);

// From now you can freely access the variable the way you want, this is just for sample demo
const a = pos[0][0];
const b = pos[0][1];
console.log(a);
console.log(b);


推荐阅读