2014-03-05 57 views
-1

我有以下功能它是工作,但我需要的數組排序正確 我會告訴你我的代碼,然後我會告訴你我的輸出,然後輸出我需要node.js中需要解決我的功能陣列輸出

編輯我完整的代碼和輸出

var app = require('express')() 
    , server = require('http').createServer(app) 
    , fs = require('fs') 
    , exec = require('child_process').exec 
    , io = require('socket.io').listen(server); 
var async = require('async'); 
server.listen(process.env.PORT); 

app.get('/', function (req, res) { 
res.sendfile(__dirname + '/client/index.html'); 
// console.log(async); 
}); 

io.sockets.on('connection', function (socket) { 
//socket.emit('dinpu', { hello: 'world' }); 
// myModule('[email protected]:asdfef'); 
socket.on('dout', function (data) { 
//module2(data); 
//console.log(JSON.parse(data.message)); 
for(i = 0; i < data.message.length; i++) { 
//data.message[i] = data.message[i].replace(/"/g, ""); 

} 
module1(data.message); 
module2(data.message); 

function module1(data) { 
console.log(data); 
} 


function async(arg, callback) { 
console.log('do something with \''+arg+'\', return 1 sec later'); 
setTimeout(function() { callback(arg * 2); }, 1000); 
} 

function module2(data) { 
function final() { console.log('Done', results); } 
var items = data; 
var results = []; 
function series(item) { 
        if(item) { 
    async(item, function(result) { 
      results.push(result); 
      return series(items.shift()); 
      }); 
     } else { 
     return final(); 
    } 
} 
    series(items.shift()); 
} 


}); 
}); 

一切工作正常,除了這部分

function final() { console.log('Done', results); } 

我得到的結果

do something with 'dfhfgh', return 1 sec later 
do something with 'gfhjghj', return 1 sec later 
do something with 'gfhjghj', return 1 sec later 
do something with 'fghjgh', return 1 sec later 
do something with 'ghjfhj', return 1 sec later 
Done [ NaN, NaN, NaN, NaN, NaN ] < the problem 

,如果引號不是在我的數組,那麼爲什麼在顯示出來的其他結果我不需要這個

+0

你是說你需要將變量存儲在數組中,而不是字符串? – srquinn

+0

我需要刪除'從陣列 – user3385289

+2

你能說清楚你的問題嗎?您將其描述爲排序問題,但這些值不會更改當前和期望之間的順序。他們只是刪除了報價單。你想達到什麼目的? –

回答

1

我需要從數組中刪除了「

好消息 - '實際上不在您的陣列中。您的數組有字符串,並且當您記錄數組時,這些字符串顯示在引號周圍。

您可以安全地使用任何這些字符串;報價不會在那裏。

+0

我理清了這一點,我會發布我的代碼上面,並告訴你我錯誤修正 – user3385289

+0

檢查我上面的代碼,因爲你看到它顯示我的結果爲'字符串',當我需要它是字符串,也得到[NAN,NAN,NAN]作爲我的最後結果輸出 – user3385289