如何在Node.js中使用array_filter

时间:2018-07-06 14:10:49

标签: php node.js

我在array_filter上遇到了小问题。我已经在PHP上成功使用了它,但是现在我想在nodejs上更改它。 任何帮助都会感谢我。

MyPHPScript

$targets = [1.2, 2.3, 3.5];
$live_coin_price = 1.3;

$targets_hit = array_filter($targets, function($target_value) use($live_coin_price){
            if($live_coin_price >= $target_value)
            return $target_value ;
        });

它将检查$ target_filter> 0与否。 这是在PHP上可行的代码。

NodeJs

我尝试使用nodejs进行类似操作。但它不起作用,需要进行更改。 它是对Binance Web套接字的一个套接字调用。我想使用我的数据创建一个套接字。

const app       =     require("express");
const mysql     =     require("mysql");
const http      =     require('http').Server(app);
const io        =     require("socket.io")(http);
const WebSocket = require("ws");


var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "cryptobot"
  });

var coinName = "";




con.connect(function(err) {
    if (err) throw err;
    con.query("SELECT tr.id, tr.title, tr.coin, tr.exchange, tr.reason, tr.buy_zone, tr.stop_loss, tr.status, tr.created_on as trade_creation_date, max(case when target_number = '1'  then target_value end) AS target1,    max(case when target_number = '1'  then target_created_on end) AS target1_created_on, max(case when target_number = '1'  then target_updated_on end) AS target1_updated_on, max(case when target_number = '2'  then target_value end) AS target2, max(case when target_number = '2'  then target_created_on end) AS target2_created_on, max(case when target_number = '2'  then target_updated_on end) AS target2_updated_on, max(case when target_number = '3' then target_value end) AS target3, max(case when target_number = '3' then target_created_on end) AS target3_created_on, max(case when target_number = '3' then target_updated_on end) AS target3_updated_on, max(case when target_number = '4'  then target_value end) AS target4, max(case when target_number = '4'  then target_created_on end) AS target4_created_on, max(case when target_number = '4'  then target_updated_on end) AS target4_updated_on, max(case when target_number = '5' then target_value end) AS target5, max(case when target_number = '5' then target_created_on end) AS target5_created_on, max(case when target_number = '5' then target_updated_on end) AS target5_updated_on FROM trades as tr inner join trades_target as tt on tr.id=tt.trades_id where status =1 GROUP BY tt.trades_id", function (err, result, fields) {
      if (err) throw err;
      var results = result;

      for (var i = 0; i < results.length; i++) {
        var row = results[i];
            coinName = row.coin;
        var stopLoss = row.stop_loss;
        var buyZone = row.buy_zone;

        var targetCreatedOn = [row.target1_created_on, row.target2_created_on, row.target3_created_on, row.target4_created_on, row.target5_created_on];
        var targetUpdatedOn = [row.target1_updated_on, row.target2_updated_on, row.target3_updated_on, row.target4_updated_on, row.target5_updated_on];

        var targets = [row.target1, row.target2, row.target3, row.target4, row.target5];

        const binanceWS = new WebSocket("wss://stream.binance.com:9443/ws/"+coinName+"@kline_1m");

        var websocketList = [];

        binanceWS.on("open", function open() {
            console.log("open action");
        });

        binanceWS.on("message", function incoming(data) {
            var datas = JSON.parse((data));
            var liveCoinPrice = datas.k.c;
            var liveCoinHighPrice = datas.k.h;
            var liveCoinLowPrice = datas.k.l;

            // send data to every websocket client
            websocketList.forEach(ws => {
                ws.send(data);

            });

            var filteredArray = targets.filter((value, index, array)=>{
                if(liveCoinPrice >= targetValue){
                    return targetValue;
                }

            });

            function targetValue(el){
                return liveCoinPrice;
            }
            console.log(targetValue);
        });

      }

    });

});


const wss = new WebSocket.Server({ port: 8081 });

wss.on("connection", function connection(ws) {

    // add ws handle to websocket list.
    websocketList.push(ws);

    ws.on("close", function close() {
        console.log("Disconnected");
    });
});

现在我想用nodejs改变它。我该怎么办?

1 个答案:

答案 0 :(得分:1)

javascript中的

array_filterfilter()

在您的代码上,targetValue不存在。函数参数上使用的变量称为value

注意array_filterfilter回调函数均期望返回值bool。不是值本身。

let targets = [1.2, 2.3, 3.5];
let liveCoinPrice = 1.3;

let targets_hit = targets.filter(function(target_value) {
  return liveCoinPrice >= target_value;
});

console.log(targets_hit);

...或者您可以使用js箭头功能使其更短:

let targets_hit = targets.filter(o => liveCoinPrice >=  o);