php - How to use array_filter in Node.js
I am facing small issue on array_filter. I have successfully used it on PHP but now i want to change this on nodejs. Any help will be thankful for me.
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 ;
});
It will check if $target_filter > 0 or not . This is workable code on PHP.
NodeJs
I tried similar with nodejs. but its not working what needs to be change. Its a socket call on Web socket of Binance. I want to create an socket by using my data .
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");
});
});
Now I want to change this with nodejs . How can i do it?
Answer
Solution:
array_filter
in javascript is.
On your code,
targetValue
does not exist. The variable used on the function parameter is calledvalue
Note: Both
array_filter
andfilter
callback function expects abool
return value. Not the value itself.Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.