Combining multiple javascript arrays into JSON using Javascript - javascript

I have a handful of javascript arrays that I have no control over as far as format or variable names. I need to combine 6 of them into a json array. I have it pretty close but can't seem to get it to iterate through both the list of variables as well as the count of array elements.
It is apparent to me that I do not remember how to handle variables or iteration in JS as well as I used to and could really use some help.
The software whose output I needs to parse generates something similar to the following:
<script type="text/javascript">
var orderNum = 'OrderNum-485';
var orderSubTotal ='130.8';
var orderTotal ='130.8';
var numOfItems ='4';
var items =new Array('Item Name 1','Item Name 2','Item Name 3','Item Name 4');
var ids =new Array('item-id-1','item-id-2','item-id-3','item-id-4');
var codes =new Array('ITEM-CODE-1','ITEM-CODE-2','ITEM-CODE-3','ITEM-CODE-4');
var qtys =new Array('1','1','1','1');
var price =new Array('12.95','46.7','1.2','69.95');
var orderTax ='0';
var orderShipping ='0';
var appliedPromoIdList ='';
var coupon ='';
var storeId ='store_id';
var activeShipPromotionCount ='';
var itemImages =new Array('http://image_url','http://image_url','http://image_url','http://image_url');
</script>
Where as the software that I need to pass data too expects the following (as an object, I managed to get it done using strings):
[
{
"item_id":"item-id-1",
"desc":"ITEM-CODE-1",
"amount":"12.95",
"quantity":"1",
"name":"Item Name 1",
"image": "http://image_url",
{
"item_id":"item-id-2",
"desc":"ITEM-CODE-2",
"amount":"46.7",
"quantity":"1",
"name":"Item Name 2",
"image": "http://image_url",
]
This is what I came up with:
<script type="text/javascript">
var orderId = orderNum;
var createItemObjects = function() {
var keys = new Array ("item_id","desc","amount","quantity","name","image");
var yahooKeys = new Array ("ids","codes","price","qtys","items","itemImages");
var item,cartItems = [];
var c = numOfItems;
var k = yahooKeys.length;
var i = 0;
item = {};
for (; i < k; i++) {
var arrayName = yahooKeys[i].toString();
var buffer = eval('' + arrayName);
item[keys[i]] = buffer[0] //Ideally this should be the full range of 0-3 so that you can see each of the 4 items.
document.write("<br />Loop: "+i);
cartItems.push(item);
}
return cartItems;
It generates correctly formatted data but 6 identical copies of the element indicated by buffer's hard coded value

Something along the lines of :
function createItemObjects(items, ids, codes, qtys, prices, images) {
var check = items.length === ids.length && ids.length === codes.length &&
qtys.length === codes.length && prices.length === images.length;
if(!check) throw 'Arrays do not match';
var results = [];
for(var i = 0; i < items.length; ++i) {
results.push({
item_id: ids[i],
desc: codes[i],
amount: parseFloat(prices[i]),
name: items[i],
quantity: qtys[i],
image: images[i]
})
}
return results;
}
var data = generate(items, ids, codes, qtys, prices, itemImages);
var json_string = JSON.stringify(data);

Related

Push different object in an array with a for loop

I have an element structured like this:
Element ->
[{values: arrayOfObject, key:'name1'}, ... ,{values: arrayOfObjectN, key:'nameN'}]
arrayDiObject -> [Object1, Object2, ... , ObjectN] //N = number of lines in my CSV
Object1 -> {x,y}
I have to take data from a big string:
cityX#substanceX#cityY#substanceY#
I thought to make it this way, but it seems like it pushes always in the same array of objects. If I put oggetto = {values: arrayDateValue, key: key}; inside the d3.csv function, instead if I put outside the function it add me only empty objects.
Here is my code:
var final = new Array();
var oggetto;
var key;
function creaDati() {
var newdate;
var arrayDateValue = new Array();
var selString = aggiungiElemento().split("#");
//selString is an array with selString[0]: city, selString[1]: substance and so on..
var citySelected = "";
var substanceSelected = "";
for (var i = 0; i < selString.length - 1; i++) {
if (i % 2 === 0) {
citySelected = selString[i];
} else if (i % 2 !== 0) {
substanceSelected = selString[i];
key = citySelected + "#" + substanceSelected;
d3.csv("/CSV/" + citySelected + ".csv", function(error, dataset) {
dataset.forEach(function(d) {
arrayDateValue.push({
x: d.newdate,
y: d[substanceSelected]
});
});
});
oggetto = {
values: arrayDateValue,
key: key
};
arrayDateValue = [];
final.push(oggetto);
}
}
}
Any idea ?
First you should make the if statement for the city and then for the key, which you seem to be doing wrong since you want the pair indexes to be the keys and the not pair to be the city, and you are doing the opposite. And then you need to have the d3.csv and push the objects outside of the if statement, otherwise in your case you are just adding elements with citySelected="".
Try something like :
for(var i = 0; i < selString.length -1; i+=2){
cittySelected = selString[i];
substanceSelected = selString[i+1];
key = citySelected + "#" + substanceSelected;
d3.csv("/CSV/"+citySelected+".csv", function(error, dataset){
dataset.forEach(function(d){
arrayDateValue.push({x: d.newdate, y: d[substanceSelected]});
});
});
oggetto = {values: arrayDateValue, key: key};
arrayDateValue = [];
final.push(oggetto);
}
It's is not the best way to do it, but it is clearer that what you are following, i think.
In the if(i % 2 == 0) { citySelected = ... } and else if(i % 2 !== 0) { substanceSelected = ... } citySelected and substanceSelected will never come together.
The values should be in one statement:
if(...) { citySelected = ...; substanceSelected = ...; }
The string can be splitted into pairs
city1#substance1, city2#substance2, ...
with a regex (\w{1,}#\w{1,}#).
Empty the arrayDateValue after the if-statement.
Hint:
var str = "cityX#substanceX#cityY#substanceY#";
function createArr(str) {
var obj = {};
var result = [];
var key = "";
// '', cityX#substanceX, '', cityYsubstanceY
var pairs = str.split(/(\w{1,}#\w{1,}#)/g);
for (var i = 0; i < pairs.length; i++) {
if(i % 2 !== 0) {
key = pairs[i];
// d3 stuff to create values
obj = {
// Values created with d3 placeholder
values: [{x: "x", y: "y"}],
// Pair
key: key
};
result.push(obj);
}
// Here should be values = [];
}
return result;
}
var r = createArr(str);
console.log(r);
May be you can do like this;
var str = "cityX#substanceX#cityY#substanceY",
arr = str.split("#").reduce((p,c,i,a) => i%2 === 0 ? p.concat({city:c, key:a[i+1]}) : p,[]);
console.log(JSON.stringify(arr));
RESOLVED-
The problem is about d3.csv which is a asynchronous function, it add in the array when it finish to run all the other code.
I make an XMLHttpRequest for each csv file and it works.
Hope it helps.

array object manipulation to create new object

var actual = [
{"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
{"country":"AUSTRIA","month":"JAN","SR":"Brad P","AC":"64","PR":"12","TR":1700},
{"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},
{"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
{"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
{"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},
{"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
{"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
{"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
];
var expect = [
{month:"JAN",val: {"UK":"24","AUSTRIA":"64","ITALY":"21"}},
{month:"FEB",val: {"UK":"14","AUSTRIA":"24","ITALY":"22"}},
{month:"MAR",val: {"UK":"56","AUSTRIA":"24","ITALY":"51"}}
];
I have array of objects which i need to reshape for one other work. need some manipulation which will convert by one function. I have created plunker https://jsbin.com/himawakaju/edit?html,js,console,output
Main factors are Month, Country and its "AC" value.
Loop through, make an object and than loop through to make your array
var actual = [
{"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
{"country":"AUSTRIA","month":"JAN","SR":"Brad P","AC":"64","PR":"12","TR":1700},
{"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},
{"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
{"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
{"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},
{"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
{"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
{"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
];
var outTemp = {};
actual.forEach(function(obj){ //loop through array
//see if we saw the month already, if not create it
if(!outTemp[obj.month]) outTemp[obj.month] = { month : obj.month, val: {} };
outTemp[obj.month].val[obj.country] = obj.AC; //add the country with value
});
var expected = []; //convert the object to the array format that was expected
for (var p in outTemp) {
expected.push(outTemp[p]);
}
console.log(expected);
Iterate through array and create new list
var actual = [
{"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
{"country":"AUSTRIA","month":"JAN","SR":"Brad P","AC":"64","PR":"12","TR":1700},
{"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},
{"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
{"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
{"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},
{"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
{"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
{"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
];
var newList =[], val;
for(var i=0; i < actual.length; i+=3){
val = {};
val[actual[i].country] = actual[i]["AC"];
val[actual[i+1].country] = actual[i+1]["AC"];
val[actual[i+2].country] = actual[i+2]["AC"];
newList.push({month: actual[i].month, val:val})
}
document.body.innerHTML = JSON.stringify(newList);
This is the correct code... as above solution will help you if there are 3 rows and these will be in same sequnece.
Here is perfect solution :
var actual = [
{"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
{"country":"AUSTRIA","month":"JAN","SR":"Brad P","AC":"64","PR":"12","TR":1700},
{"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},
{"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
{"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
{"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},
{"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
{"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
{"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
];
var tmpArray = [];
var obj =[];
for(var k=0; k<actual.length; k++){
var position = tmpArray.indexOf(actual[k].month);
if(position == -1){
tmpArray.push(actual[k].month);
val = {};
for(var i=0; i<actual.length; i++){
if(actual[i].month == actual[k].month){
val[actual[i].country] = actual[i]["AC"];
}
}
obj.push({month: actual[k].month, val:val});
}
}

selecting particular keys from json data

I have a array as:
var cols = ["ticker", "highPrice", "lowPrice","lastPrice"] // dynamic
Json data comming from backend as:
info = {ticker: "AAPL", marketCap: 2800000000, lowPrice: 42.72, highPrice: 42.84}
suppose I want to select market cap then I can do info.marketCap. But I want to select only those json values which keys are equals to cols i.e. info.ticker, info.highPrice, info.lowPrice
and assign N/A to those which is undefined in json but present in cols array i.e info.lastPrice = "N/A"
Note: cols changes from time to time
Here is what I have got so far
SyScreener.fillScreenerResult = function(info) {
var cols = ["ticker", "highPrice", "lowPrice", "openPrice", "lastPrice", "currentVol", "avgVol"];
var data = [];
for(var i=0; i<info.length; i++) {
var jsonKeys = Object.keys(info[i]);
for(var j=0; j<jsonKeys.length; i++) {
if(cols.contains(jsonKey[j])) {
// TODO something like - data.push([info[i].jsonKey[j])
} else {
// TODO something like - info[i].colsValue = "N/A"
}
}
}
SyUtils.createDataTable("screener_result", data);
};
do you mean something like this:
var cols = ["ticker", "highPrice", "lowPrice","lastPrice"];
info = {ticker: "AAPL", marketCap: 2800000000, lowPrice: 42.72, highPrice: 42.84};
for(var c = 0, clen = cols.length; c < clen; c++) {
if( !(cols[c] in info) ) {
console.log("N/A");
}
else {
console.log(info[cols[c]]);
}
}
Demo:: jsFiddle
I may not be reading your question correctly but from my understanding I might suggest something like this.
for (var i=0; i<cols.length; i++) {
var fieldName = cols[i];
if (!info.hasOwnProperty(fieldName)) {
info[fieldName] = 'N/A';
}
}
This simply iterates through each field name in cols and checks if it is a property of the info JSON object. If it isn't already present the loop adds the property with a value of 'N/A'.
var cols = ["ticker", "highPrice", "lowPrice","lastPrice"]
var info = {ticker: "AAPL", marketCap: 2800000000, lowPrice: 42.72, highPrice: 42.84}
var output = {};
for(each in info) {
var index = cols.indexOf(each)
if(index != -1) {
output[each] = info[each];
//remove extra already checked element
cols.splice(index,1)
}
}
//append remaining keys
for(var i=0;i<cols.length;i++) {
output[cols[i]] = "N/A"
}
console.log(output)
//output Object {ticker: "AAPL", lowPrice: 42.72, highPrice: 42.84, lastPrice: "N/A"}
First thing, you nedd deserialize you JSON data.
To do this using jQuery use the function parseJson that you can find here
http://api.jquery.com/jquery.parsejson/
once you deserialized it, you can do whatever you want with this data since you manipulate it as a plain javascript array. Hope this helps.

Combine Arrays in Order JavaScript

I have a few arrrays which I am trying to combine in a certain order. For example, lets say I have three arrays:
var arrayOne = [1a, 1b, 1c];
var arrayTwo = [2a, 2b, 2c];
var arrayThree [3a, 3b, 3c];
how would I get something like this?
var combinedArray = [1a, 2a, 3a, 1b, 2b, 3b, 1c, 2c, 3c]
EDIT
Ok, I am going to add a little more to my story here since I have gotten some great responses and answers from everyone. Maybe this will be clearer. I have a SharePoint list which I am getting all of my information from. I am using SPServices to get my data from the list. I am also getting the version history of each list item and placing them into arrays so I can pull the data out of the SPServices loop and use it outside of the SharePoint stuff so I can try to order and display it as I want. (Please do not move this to SharePoint ).
This is all going to have to happen at runtime. So here is the first part. I am declaring global variables for each one of my list items:
var proModified6 = new Array();
var proHistory = new Array();
var assignedTo = new Array();
var startDate = new Array();
var endDate = new Array();
var status = new Array();
var project = new Array();
var history = new Array();
var listItems = new Array();
var regex = new RegExp("");
var i = 0;
Then I am filling the arrays with the SharePoint list info ( I am not going to put them all but each has a call like this one)
$().SPServices({
operation: "GetVersionCollection",
async: false,
webURL: "http://devchrisl01/test",
strlistID: "NewProjects",
strlistItemID: proID[i],
strFieldName: "Title",
completefunc: function (xdata, Status) {
$(xdata.responseText).find("Version").each(function() {
//alert(xdata.responseXML.xml);
var xitem = $(this);
var ID = xitem.attr('ID');
var Project = xitem.attr('Title');
var Modified = xitem.attr('Modified').split('T')[0];
var ModifiedTime = xitem.attr('Modified').substring(11, 19);
//var modifiedUl = "<td><b>" + Modified + " " + ModifiedTime + "</b></td>";
//$('#versionList'+i+'').append(modifiedUl);
project.push(Project);
proModified2.push(Modified + ModifiedTime)
// just looking at my data here not really part of my problem
var data = "<tr><td><b>" + Modified + " " + ModifiedTime + "</b></td><td>" + Project + "</td></tr>";
$('#versionList'+i+'').append(data);
});
}
});
After is where my question has come into play. I am getting all of my data back I need. I have not found a better way to store all of the list information and pull it out of the SPServices besides using an array for each. The kicker is I am not going to know how many arrays there are going to be or how long. Eventually this is going to be dynamic. (PAIN IN THE ASS ) so here are the arrays:
var modifiedDate = [proModified1, proModified2, proModified3, proModified4, proModified5, proModified6];
var projectHistory = [history];
var projectTitle = [project];
var projectAssignedTo = [assignedTo];
var projectStartDate = [startDate];
var projectEndDate = [endDate];
var projectStatus = [status];
Each one is going to be different. There is not going to be the same amount of them either for each user. I have just made my list static to build on first. List dynamics will be another question later :0
Is there going to be a way for me to do with these arrays like I asked in my simple example? See why I went simple first lol.
Edit: Updated comparator as Salmon pointed out that it should return -1, 0 or 1.
Try below,
var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b", "3c"];
var combinedArray = arrayOne
.concat(arrayTwo, arrayThree) //Concatenate the array.. can add any number of arrays
.sort(function(a, b) { //Custom comparator for your data
var a1 = a.charAt(1);
var b1 = b.charAt(1);
if (a1 == b1) return 0;
if (a1 < b1) return -1;
return 1;
});
Note: The custom comparator function is very specific to your data. Update the function as you need.
DEMO
From my understanding, you're looking for a zip function, like in python. There's no such built-in in javascript, but it's easy to write, for example:
zip = function() {
var
args = [].slice.call(arguments, 0),
len = Math.max.apply(Math, args.map(function(x) { return x.length })),
out = [];
for (var i = 0; i < len; i++)
out.push(args.map(function(x) { return x[i] }));
return out;
}
Applied to your arrays
var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b", "3c"];
zipped = zip(arrayOne, arrayTwo, arrayThree);
this creates a nested array:
[["1a", "2a", "3a"], ["1b", "2b", "3b"], ["1c", "2c", "3c"]]
which you can use as is or convert to a flat one:
flat = [].concat.apply([], zipped)
If the length of the three arrays will always be the same of each other:
var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b", "3c"];
var newArray = [];
for (var i = 0, len = arrayOne.length; i < len; i++) {
newArray.push(arrayOne[i], arrayTwo[i], arrayThree[i]);
}
console.log(newArray); //1a,2a,3a,1b,2b,3b,1c,2c,3c
But if your arrays won't always be the same length, we will need to use the greatest length of the three arrays and skip missing indexes, so it will be like this:
var arrayOne = ["1a", "1b", "1c", "1d"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b"];
var newArray = [];
var len = Math.max(arrayOne.length, arrayTwo.length, arrayThree.length);
for (var i = 0; i < len; i++) {
if (i in arrayOne) {
newArray.push(arrayOne[i]);
}
if (i in arrayTwo) {
newArray.push(arrayTwo[i]);
}
if (i in arrayThree) {
newArray.push(arrayThree[i]);
}
}
console.log(newArray); //1a,2a,3a,1b,2b,3b,1c,2c,1d
The reason to use i in arr (as difference of other checks), is because this will allow you to keep any falsish value 0,"",false,null,undefined,NaN intact. Otherwise it will be very hard to leave them alone (specially undefined values inside your array)
Performance tests between my answer and the currently most upvoted answer
I'd suggest using the default JS sort() method with a callback of some kind. If the elements if the array are all like /[0-9][a-z]/, this will give you the desired result:
//concat arrays first, of course
function specialSort(a,b)
{
if(a[1] < b[1])
{
return -1;
}
if(a[1] > b[1])
{
return 1;
}
return 0;
}
//using wsanville's expample: combinedArray = ["1a", "1b", "1c", "2a", "2b", "2c", "3a", "3b", "3c"]
combinedArray = combinedArray.sort(specialSort);
//result: combinedArray = ["1a", 2a", "3a", "1b", "2b", "3b", "1c", "2c", "3c"]
Hope this works for you...
You can simply do:
var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c", "2d", "2e"];
var arrayThree = ["3a", "3b", "3c", "3d"];
var combinedArray = [];
for (var i = 0, j = Math.max(arrayOne.length, arrayTwo.length, arrayThree.length); i < j; i++) {
if (arrayOne.length) combinedArray.push(arrayOne.shift());
if (arrayTwo.length) combinedArray.push(arrayTwo.shift());
if (arrayThree.length) combinedArray.push(arrayThree.shift());
}
alert(combinedArray.join(", "));​
This should probably work for variable length arrays. Demo here.

Loop through two Array in JavaScript and Constructing a function

I have Two Arrays in Javascript as shown below :
Array one = new Array();
one.push(20061001);
one.push(20061002);
one.push(20061003);
one.push(20061120);
Array two = new Array();
two.push(3.0);
two.push(3.1);
two.push(3.2);
two.push(3.3);
Now Some how i need to loop through this Array and construct a function as shown
function NoisyData() {
return "" +
"Date,A\n" +
"20061001,3.0\n" +
"20061002,3.1\n" +
"20061003,3.2\n" +
"20061120,4.0\n" ;
}
Please help me as how to do this ??
How about this?
var one = new Array();
one.push(20061001);
one.push(20061002);
one.push(20061003);
one.push(20061120);
var two = new Array();
two.push('3.0');
two.push('3.1');
two.push('3.2');
two.push('3.3');
function NoisyData() {
var result = "Date,A\n";
for(var i = 0; i < one.length;i++){
result += one[i] + "," + two[i] + "\n";
}
return result;
}
alert(NoisyData());
The faster way for long array is :
var one = new Array();
one.push(20061001);
one.push(20061002);
one.push(20061003);
one.push(20061120);
var two = new Array();
two.push(3.0);
two.push(3.1);
two.push(3.2);
two.push(3.3);
function NoisyData() {
var ret = [];
ret.push("Date,A");
for (var i=0;i<one.length;i++){
ret.push(one[i]+','+two[i]);
}
return ret.join('\n');
}
alert(NoisyData());
Your code can be a lot shorter. You can't type variables (like Array one) in javascript. To declare an Array most of the time an Array literal is sufficient.
If your arrays have the same length, you can use the code hereby to combine them into the string you need:
var one = [20061001,20061002,20061003,20061120]
, two = [3.0,3.1,3.2,3.3]
, combine = function(a1,a2){
var i = -1, len = a1.length, res = ['Date,A'];
while(++i < len){
res.push(a1[i]+','+a2[i].toPrecision(2));
}
return res.join('\n');
}(one,two);
Try it # http://jsfiddle.net/KooiInc/jdn6U/
You mean
function NoisyData() {
var txt = "Date,A\n"
for (var i=0, n=one.length;i<n;i++) {
txt += one[i]+","+two[i]+"\n"
}
return txt
}
UPDATE based on KooiInc's posts:
<script>
var one = [20061001,20061002,20061003,20061120]
, two = [3.0,3.1,3.2,3.3]
, combined = function(res,two){
var i = one.length;
while(i--){
res[i]+=','+two[i].toPrecision(2);
}
res.splice(0,0,'Date,A');
return res.join('\n')
}(one.slice(0),two);
alert(combined);
</script>
Instead of one.slice(0) one.clone() can be implemented as
Array.prototype.clone = function() { return this.slice(0); }
or just pass one itself instead if it is OK to modify the original array

Categories

Resources