Javascript 如何将值从一个数组添加到另一个数组
编辑#3 为了获得更好的帮助(感谢您的耐心),我想结合以下两个脚本: 脚本1:Javascript 如何将值从一个数组添加到另一个数组,javascript,arrays,Javascript,Arrays,编辑#3 为了获得更好的帮助(感谢您的耐心),我想结合以下两个脚本: 脚本1: //get csv file and set up array d3.csv('../mapdata/mapdatatest.csv', function (csv) { var rid = [], lat = [], lon = [], pinclr = [],
//get csv file and set up array
d3.csv('../mapdata/mapdatatest.csv', function (csv) {
var rid = [],
lat = [],
lon = [],
pinclr = [],
name = [],
str = [],
citystzip = [],
phone = [],
lastinspturl = [],
lastinspctdt = [];
csv.map(function (d) {
rid.push(d.rid).toString();
lat.push(d.lat).toString();
lon.push(d.lon).toString();
pinclr.push(d.pinclr).toString();
name.push(d.name).toString();
str.push(d.str).toString();
citystzip.push(d.citystzip).toString();
phone.push(d.phone).toString();
lastinspturl.push(d.lastinspturl).toString();
lastinspctdt.push(d.lastinspctdt).toString();
for (i = 0; i < rid.length; i++) {
var points = ('"' + lat[i] + "," + lon[i] + '"');
}
});
});
需要
var points = {
"points": [
THE "point" VALUES FROM THE SCRIPT 1 loop]
};
我理解了这个概念,似乎无法正确理解语法…尝试了所有的建议,push();,阅读了很多文章,样品…我需要这个10小时前,任何帮助将不胜感激。如果我有足够的代表,我会投票支持你:)谢谢你,谢谢你,谢谢你。也许这会奏效,但我不知道你的变量rid、lat和long是什么。也许你可以把它寄出去。要在Chrome或Firefox中查看变量,可以执行以下操作:
console.log(JSON.stringify(rid);
按F12键查看控制台
var points={};
points.points=[];
for (i = 0; i < rid.length; i++) {
points.points.push('"' + rid[i].lat + "," + rid[i].lon + '"');
}
var points={};
点数。点数=[];
对于(i=0;i
也许这会起作用,但我不知道您的变量rid、lat和long是什么。也许你可以把它寄出去。要在Chrome或Firefox中查看变量,可以执行以下操作:
console.log(JSON.stringify(rid);
按F12键查看控制台
var points={};
points.points=[];
for (i = 0; i < rid.length; i++) {
points.points.push('"' + rid[i].lat + "," + rid[i].lon + '"');
}
var points={};
点数。点数=[];
对于(i=0;i
我很难理解你的问题。这是否有帮助:
var points = {
"points": [
"47.15211, -97.570039",
"48.625045, -101.375369",
"48.39679, -101.052669"
]
};
console.log(points.points);
var array = points.points;
var array_len = array.length;
for(var i = 0; i < array_len; ++i)
{
var str = array[i];
console.log(str);
}
--output:--
[ '47.15211, -97.570039',
'48.625045, -101.375369',
'48.39679, -101.052669' ]
47.15211, -97.570039
48.625045, -101.375369
48.39679, -101.052669
然后,只需将func1和func2放在单独的文件中,并将它们都包含在html页面中:
<script type='text/javascript' src='js2.js'></script>
<script type='text/javascript' src='js1.js'></script>
现在,如果您只想将一件事传递给do_other_stuff()函数,例如,您的对象(周围有大括号的东西称为“对象”),您可以像这样重写脚本:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
js.js
function do_stuff() {
var points = {
"points": [
"47.15211, -97.570039",
"48.625045, -101.375369",
"48.39679, -101.052669" ]
};
do_other_stuff(points);
}
do_stuff();
然后重写do_other_stuff(),如下所示:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
js2.js
function do_other_stuff(points_obj) {
//do stuff with points_obj, e.g.
alert( points_obj.points[0] );
}
在本例中,脚本不在任何html元素上运行,因此无需等待页面加载
====
查看以下注释是否有帮助:1) 此循环:
for (i = 0; i < rid.length; i++) {
var points = ('"' + lat[i] + "," + lon[i] + '"');
}
2) 你用引号做的事情真的很难看。如果您只是想将一些数字转换为字符串,可以执行以下操作:
var point = lat[i] + ", " + lon[i];
js无法将数字和字符串相加,因此js假设您正在尝试创建字符串,js将数字转换为字符串,然后将字符串相加。看看这个:
var str = 3 + ', ' + 2;
var arr = [str];
console.log(arr);
--output:--
[ '3, 2' ]
3) 您可能希望执行以下操作:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
============
1) 调用函数时,js将函数调用与函数定义对齐:
do_stuff(10, 20, 30) <----function call
function do_stuff( x, y, z) {...} <---function definition
2) 然后在函数内部,使用变量x、y和z来引用这些值
3) 在我发布的代码中,函数调用和函数定义如下所示:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
所以js做这个任务:
var the_points = points;
点是类似于['10,20','100,200']的数组,所以赋值等于:
var the_points = ['10, 20', '100, 200']
在函数内部,您使用_点引用数组。我很难理解您的问题。这是否有帮助:
var points = {
"points": [
"47.15211, -97.570039",
"48.625045, -101.375369",
"48.39679, -101.052669"
]
};
console.log(points.points);
var array = points.points;
var array_len = array.length;
for(var i = 0; i < array_len; ++i)
{
var str = array[i];
console.log(str);
}
--output:--
[ '47.15211, -97.570039',
'48.625045, -101.375369',
'48.39679, -101.052669' ]
47.15211, -97.570039
48.625045, -101.375369
48.39679, -101.052669
然后,只需将func1和func2放在单独的文件中,并将它们都包含在html页面中:
<script type='text/javascript' src='js2.js'></script>
<script type='text/javascript' src='js1.js'></script>
现在,如果您只想将一件事传递给do_other_stuff()函数,例如,您的对象(周围有大括号的东西称为“对象”),您可以像这样重写脚本:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
js.js
function do_stuff() {
var points = {
"points": [
"47.15211, -97.570039",
"48.625045, -101.375369",
"48.39679, -101.052669" ]
};
do_other_stuff(points);
}
do_stuff();
然后重写do_other_stuff(),如下所示:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
js2.js
function do_other_stuff(points_obj) {
//do stuff with points_obj, e.g.
alert( points_obj.points[0] );
}
在本例中,脚本不在任何html元素上运行,因此无需等待页面加载
====
查看以下注释是否有帮助:1) 此循环:
for (i = 0; i < rid.length; i++) {
var points = ('"' + lat[i] + "," + lon[i] + '"');
}
2) 你用引号做的事情真的很难看。如果您只是想将一些数字转换为字符串,可以执行以下操作:
var point = lat[i] + ", " + lon[i];
js无法将数字和字符串相加,因此js假设您正在尝试创建字符串,js将数字转换为字符串,然后将字符串相加。看看这个:
var str = 3 + ', ' + 2;
var arr = [str];
console.log(arr);
--output:--
[ '3, 2' ]
3) 您可能希望执行以下操作:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
============
1) 调用函数时,js将函数调用与函数定义对齐:
do_stuff(10, 20, 30) <----function call
function do_stuff( x, y, z) {...} <---function definition
2) 然后在函数内部,使用变量x、y和z来引用这些值
3) 在我发布的代码中,函数调用和函数定义如下所示:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
function do_stuff(the_points) {
//Do all your deCarta stuff here
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
console.log(the_points); //You have access to the points array
obj = {"points": the_points};
do_stuff(points)
function do_stuff(the_points) {...}
所以js做这个任务:
var the_points = points;
点是类似于['10,20','100,200']的数组,所以赋值等于:
var the_points = ['10, 20', '100, 200']
在函数内部,您可以使用_点来引用数组。您可以使用类似的方法来遍历数组中的每一对:
var points = [ "47.15211, -97.570039", "48.625045, -101.375369", "48.39679, -101.052669"];
points.forEach(function (point) {
point = point.match(/^"([0-9\.]+)\s*,\s*([0-9\.]+)"$/);
console.log('"' + point[0] + '", "' + point[1] + '"');
});
或者类似的东西,如果你想把它们放在自己的数组中:
var points = [ "47.15211, -97.570039", "48.625045, -101.375369", "48.39679, -101.052669"],
lat = [], lon = [];
points.forEach(function (point) {
point = point.match(/^"([0-9\.]+)\s*,\s*([0-9\.]+)"$/);
lat.push(point[0]);
lon.push(point[1]);
});
lat.forEach(function (lat, id) {
console.log('"' + lat + '", "' + lon[id] + '"');
});
甚至:
lon.forEach(function (lon, id) {
console.log('"' + lat[id] + '", "' + lon + '"');
});
还有,有人在这里发表评论说,当你们把它重新连接在一起时,我不应该使用split。如果您不想让它们像这样分开,您可以始终使用:
points.points = points.points.map(function (point) {
return point.replace(/^"([0-9\.]+)\s*,\s*([0-9\.]+)"$/, '"$1", "$2"');
});
您可以使用类似这样的方法来遍历阵列中的每一对:
var points = [ "47.15211, -97.570039", "48.625045, -101.375369", "48.39679, -101.052669"];
points.forEach(function (point) {
point = point.match(/^"([0-9\.]+)\s*,\s*([0-9\.]+)"$/);
console.log('"' + point[0] + '", "' + point[1] + '"');
});
或者类似的东西,如果你想把它们放在自己的数组中:
var points = [ "47.15211, -97.570039", "48.625045, -101.375369", "48.39679, -101.052669"],
lat = [], lon = [];
points.forEach(function (point) {
point = point.match(/^"([0-9\.]+)\s*,\s*([0-9\.]+)"$/);
lat.push(point[0]);
lon.push(point[1]);
});
lat.forEach(function (lat, id) {
console.log('"' + lat + '", "' + lon[id] + '"');
});
甚至:
lon.forEach(function (lon, id) {
console.log('"' + lat[id] + '", "' + lon + '"');
});
还有,有人在这里发表评论说,当你们把它重新连接在一起时,我不应该使用split。如果您不想让它们像这样分开,您可以始终使用:
points.points = points.points.map(function (point) {
return point.replace(/^"([0-9\.]+)\s*,\s*([0-9\.]+)"$/, '"$1", "$2"');
});
“将csv读入数组的独立代码非常有效。”基于您发布的“数组”,我不同意。您应该将csv文件中的数据组合成一个结构,允许您无需进一步处理即可访问内容。发布您的csv代码。您似乎想了解如何向现有数组添加值。在这种情况下,您需要使用
arr.push(新值)
。请看一看JavaScript教程,该教程涵盖了有关数组的基础知识,例如:.Felix,感谢您提供的链接!我已经将其添加到书签中。“将csv读入数组的独立代码非常有效。”基于您发布的“数组”,我不同意。您应该将csv文件中的数据组合到一个结构中,这样您就可以访问内容,而无需进行任何其他操作