php - How to use array_filter in Node.js
Get the solution ↓↓↓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: Botharray_filter
andfilter
callback function expects abool
return value. Not the value itself.
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);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: you must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
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.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Node.js
Node.js is an open source server-side framework built on top of the Google Chrome JavaScript Engine. The number of sites using NodeJS has increased by 84,000. It is one of the busiest cross-platform JavaScript runtimes. Node.js is an asynchronous, single-threaded, non-blocking I / O model that makes it lightweight and efficient. The Node.js package ecosystem, npm, is also the world's largest open source library ecosystem.
https://nodejs.org/
MySQL
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.