[英]How to add value from one array to another array
edit #3 in the interest of getting better help (THANK YOU for the patience) i want to combine these two scripts: 编辑#3为了获得更好的帮助(感谢您的耐心等待),我想将以下两个脚本结合起来:
SCRIPT 1: 脚本1:
//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] + '"');
}
});
});
SCRIPT 2: 脚本2:
deCarta.Core.Configuration.clientName = Config.clientName;
deCarta.Core.Configuration.clientPassword = Config.clientPassword;
var center = new deCarta.Core.Position(Config.position);
var pinOverlay = new deCarta.Core.MapOverlay({
name: "Pins"
});
window.map = new deCarta.Core.Map({
id: "mapContainer",
autoResize: true,
zoom: 11,
center: center,
onReady: function (map) {
map.addLayer(pinOverlay);
postPins();
}
});
function postPins() {
var points = {
"points": [
//i have typed in these values for testing purposes only
"47.15211, -97.570039",
"48.625045, -101.375369",
"48.39679, -101.052669"]
};
for (var i = 0; i < points.points.length;) {
pos = new deCarta.Core.Position(points.points[i]);
pin = pin = new deCarta.Core.Pin({
position: center.clone(),
text: 'pin: ' + (points.points[i]),
position: pos
// imageSrc: 'img/pin.png'
});
pinOverlay.addObject(pin);
i++;
}
var view = new deCarta.Core.BoundingBox(points.points);
var centerAndZoom = view.getIdealCenterAndZoom(window.map);
map.zoomTo(centerAndZoom.zoom);
map.centerOn(centerAndZoom.center);
}
THE RESULT I AM TRYING TO ACHIEVE: 我想要达到的结果:
instead of using typed in values as i'm doing in SCRIPT 2 -- i want those values to be fed in from SCRIPT 1. 而不是像在SCRIPT 2中那样使用输入的值-我希望从SCRIPT 1中输入这些值。
so 所以
var points = {
"points": [
//i have typed in these values for testing purposes only
"47.15211, -97.570039",
"48.625045, -101.375369",
"48.39679, -101.052669"]
};
needs to be 需要是
var points = {
"points": [
THE "point" VALUES FROM THE SCRIPT 1 loop]
};
i get the concept, can't seem to get the syntax right...tried all the suggestions, the push();, read a lot of articles, samples...i needed this 10 hours ago, any assistance will be greatly appreciated. 我了解了这个概念,似乎无法正确理解语法...尝试了所有建议,push();,阅读了很多文章,示例...我在10个小时前就需要了,任何帮助都会很大赞赏。 i'd vote you up if i had enough rep yet :) thank you, thank you, thank you. 如果我有足够的代表,我会投票给你:)谢谢,谢谢,谢谢。
I'm having a hard time understanding your questions. 我很难理解你的问题。 Does this help at all: 这是否有帮助:
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
====== ======
i built on another page: 我建立在另一页上:
That is troublesome. 那很麻烦。 Are you aware that the web is stateless ? 您是否知道网络是无状态的 ? That means that once a user leaves a page, no data is saved on the user's computer. 这意味着,一旦用户离开页面,就不会在用户的计算机上保存任何数据。 There are some ways around that: you can save small bits of information in cookies, or a page can send the data to a server side script, and then the server side script can save the data in a file or a database. 可以通过一些方法解决:您可以将少量信息保存在cookie中,或者页面可以将数据发送到服务器端脚本,然后服务器端脚本可以将数据保存在文件或数据库中。
On the other hand, if by "on another page" you mean another javascript file, then start simpler. 另一方面,如果“在另一页上”是指另一个javascript文件,则可以更简单地开始。 Combine both javascript files into one file and get that to work, eg: 将两个javascript文件合并为一个文件,然后使其生效,例如:
func1(a, b) = {
....
return results;
}
func2(x, y, z) = {
info = func1(x, y) + z
//do something with info
}
Then it's a just a matter of putting func1 and func2 into separate files and including both of them in an html page: 然后,只需将func1和func2放入单独的文件中,并将它们都包含在html页面中,就可以了:
<script type='text/javascript' src='js2.js'></script>
<script type='text/javascript' src='js1.js'></script>
Just make sure you get the order right: if function in js1.js calls a function defined in js2.js, then js2.js needs to be included first. 只要确保您的命令正确无误:如果js1.js中的函数调用了js2.js中定义的函数,则需要首先包含js2.js。
==== ====
html.html html.html
<html>
<head>
<title>Test</title>
<script type='text/javascript' src='js2.js'></script>
<script type='text/javascript' src='js.js'></script>
<style type='text/css'>
.colorText {
color: blue;
}
.surprise {
background: red;
}
</style>
</head>
<body>
<div id="show_results" class="colorText">Hello world</div>
</body>
</html>
js.js js.js
function do_stuff(x, y, z) {
//send two of this function's arguments to another function
//defined in another script:
var results = do_other_stuff(x, y);
return results + z;
}
//This function will execute once the html page loads:
window.onload = function() {
var my_results = do_stuff(10, 20, 30);
alert("Inserting results in <div>");
//The following div won't exist until after the page loads:
var div = document.getElementById('show_results');
div.innerHTML = my_results;
}
If the window.onload thing is too confusing, just get rid of it and use alert()'s to show the results (or any other info you are interested in). 如果window.onload事情太混乱了,那就摆脱它,并使用alert()显示结果(或您感兴趣的任何其他信息)。
js2.js js2.js
function do_other_stuff(x, y) {
return x+y;
}
Now, if you want to pass just one thing to the do_other_stuff() function, eg your object (things with braces around them are called 'objects'), you can rewrite your scripts like this: 现在,如果您只想将一件事传递给do_other_stuff()函数,例如您的对象(带有括号的对象称为“对象”),则可以像下面这样重写脚本:
js.js 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();
Then rewrite do_other_stuff() to look like this: 然后重写do_other_stuff()如下所示:
js2.js js2.js
function do_other_stuff(points_obj) {
//do stuff with points_obj, e.g.
alert( points_obj.points[0] );
}
In this example, the scripts aren't operating on any of the html elements, so there is no need to wait for the page to load. 在此示例中,脚本未对任何html元素进行操作,因此无需等待页面加载。
==== ====
See if the following comments help: 查看以下注释是否有帮助:
1) This loop: 1)这个循环:
for (i = 0; i < rid.length; i++) {
var points = ('"' + lat[i] + "," + lon[i] + '"');
}
is equivalent to: 等效于:
var points = '"' + lat[rid.length] + "," + lon[rid.length] + '"';
2) The thing you are doing with the quotes there is really ugly. 2)您使用引号所做的事情确实很丑。 If you are just trying to turn some numbers into a string, you can do this: 如果您只是尝试将一些数字转换为字符串,则可以执行以下操作:
var point = lat[i] + ", " + lon[i];
js can't add a number and a string together, so js makes the assumption that you are trying to create a string, and js converts the number to a string then adds the strings together. js不能将数字和字符串加在一起,因此js假设您正在尝试创建字符串,然后js将数字转换为字符串,然后将字符串加在一起。 Check this out: 看一下这个:
var str = 3 + ', ' + 2;
var arr = [str];
console.log(arr);
--output:--
[ '3, 2' ]
3) You probably want to do something like this: 3)您可能想要执行以下操作:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
4) Then to pass the points array to your deCarta stuff, you can do this: 4)然后将点数组传递给您的deCarta素材,您可以执行以下操作:
var points = []
for (i = 0; i < rid.length; i++) {
points.push( lat[i] + ", " + lon[i] );
}
do_stuff(points);
And then you would define do_stuff() like this: 然后您将像这样定义do_stuff():
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};
======= =======
1) When you call a function, js lines up the function call with the function definition: 1)调用函数时,js将函数调用与函数定义对齐:
do_stuff(10, 20, 30) <----function call
function do_stuff( x, y, z) {...} <---function definition
Then javascript does these assignments: 然后,javascript执行以下分配:
var x = 10;
var y = 20;
var z = 30;
2) Then inside the function, you use the variables x, y, and z to refer to those values. 2)然后在函数内部,使用变量x,y和z引用这些值。
3) In the code I posted, the function call and function definition look like this: 3)在我发布的代码中,函数调用和函数定义如下所示:
do_stuff(points)
function do_stuff(the_points) {...}
So js does this assignment: 因此,js执行此分配:
var the_points = points;
And points is just some array like ['10, 20', '100, 200'], so that assignment is equivalent to: 而且点只是诸如['10,20','100,200']之类的数组,因此赋值等效于:
var the_points = ['10, 20', '100, 200']
And inside the function you use the_points to refer to the array. 在函数内部,您可以使用the_points引用数组。
You can use something like this to run through each pair in the array: 您可以使用类似的方法遍历数组中的每一对:
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] + '"');
});
Or something like this if you're wanting to put them in their own arrays: 或类似这样的东西,如果您想将它们放在自己的数组中:
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] + '"');
});
Or even: 甚至:
lon.forEach(function (lon, id) {
console.log('"' + lat[id] + '", "' + lon + '"');
});
Also, someone commented on here and said that I shouldn't be using split for this when you're joining it back together. 另外,有人在这里发表评论,并说当您将其重新结合在一起时,我不应该为此使用split。 If you're not looking to have them separated like this, you can always use: 如果您不希望像这样将它们分开,则可以始终使用:
points.points = points.points.map(function (point) {
return point.replace(/^"([0-9\.]+)\s*,\s*([0-9\.]+)"$/, '"$1", "$2"');
});
Maybe this will work but I don't know what your variables rid, lat and long are. 也许这会起作用,但我不知道您的变量rid,lat和long是什么。 Maybe you could post it. 也许您可以发布它。 To see the variable in Chrome or Firefox you can do: 要在Chrome或Firefox中查看变量,可以执行以下操作:
console.log(JSON.stringify(rid);
Press F12 to see the console. 按F12查看控制台。
var points={};
points.points=[];
for (i = 0; i < rid.length; i++) {
points.points.push('"' + rid[i].lat + "," + rid[i].lon + '"');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.