Parse string (node js ) .Find array of numbers into string - node.js

\n54766392632990,178.32.243.13,wfsdsfsdfs23432,\n54766393632990,178.32.243.13,
Above u can see example of string which I want to parse.. I want to get array if numbers which exist between (\n....,178.32.243.13) .. In this example it will be smth like :
[54766392632990,54766393632990] - how to make it

Please run this script it full file your requirement
var ss = "\n54766392632990,178.32.243.13,wfsdsfsdfs23432,\n54766393632990,178.32.243.13,"
var ddd = ss.split(",")
console.log(ddd)
var dfd = []
ddd.forEach(function(res){
if(res.startsWith("\n"))
{
dfd.push(res.replace("\n",""))
}
})
console.log(dfd)
Result [ '54766392632990', '54766393632990' ]

"\n54766392632990,178.32.243.13,wfsdsfsdfs23432,\n54766393632990,178.32.243.13,"
.split("\n")
.filter((n)=> n!== "")
.map(n=> parseInt(n.split(",")[0]))

You can do something like this to parse this string
let s = "\n54766392632990,178.32.243.13,wfsdsfsdfs23432,\n54766393632990,178.32.243.13,"
s = s.split("\n");
let array = [];
for(let i=0;i<s.length;i++) {
let v = s[i].split(",178.32.243.13,");
for(let j=0;j<v.length;j++) {
if(!isNaN(parseInt(v[j]))) {
array.push(v[j]);
}
}
}
console.log(array);

Related

Get Date from Nested Array of Objects and set that date as an Key in Node.js

I'm using Node.js I have data like this:
const data= [
{"id":"1","date":"2022-09-07T15:56:32.279Z","req_id":"98"},
{"id":"2","date":"2022-09-08T15:48:19.075Z","req_id":"97"},
{"id":"3","date":"2022-09-06T15:48:19.073Z","req_id":"96"}
{"id":"4","date":"2022-09-06T15:48:19.073Z","req_id":"96"}
]
I want data in this format:
expected Output:
"2022-09-06":[
{"id":"4","date":"2022-09-06T15:48:19.073Z","req_id":"96"},
{"id":"3","date":"2022-09-06T15:48:19.073Z","req_id":"96"}
]
"2022-09-08":[
{"id":"2","date":"2022-09-08T15:48:19.075Z","req_id":"97"}
]
"2022-09-07":[
{"id":"1","date":"2022-09-07T15:56:32.279Z","req_id":"98"}
]
Assuming the dates are always in the same format, I would do something like this:
function mapData(data){
// returns the given date as an string in the "%dd-%mm-%yyyy" format
const getDateWithoutTime = (dateString) => dateString.split("T")[0];
const mappedData = [];
for(const req of data){
const formattedDate = getDateWithoutTime(req.date);
// if the key already exists in the array, we add a new item
if(mappedData[formattedDate]) mappedData[formattedDate].push(req);
// if the key doesn't exist, we create an array with that single item for that key
else mappedData[formattedDate] = [req];
}
return mappedData;
}
Straightforward solution using regex:
let result = new Map();
for (const item of data) {
let date = item['date'].match(/\d{4}-\d{2}-\d{2}/)[0];
let items = result.get(date) || [];
items.push(item);
result.set(date, items)
}

How to add Numbers In Arrays format if we have a word in 3 files using nodejs

I want to ask a question, what is it, I have three files, if there is a Word COMMON in those files then it should be printed like this [1,2 3], otherwise, if there is a word in 1 and 2 then it should be printed like this [1 2] , I Tried to PUSH ARRAY but it's not happening
Here Is My Code:
let Page1data = filtered.map((val) => {
let data = {};
if (Page1.includes(val)) {
data[val] = ["1"];
}
if (Page2.includes(val)) {
data[val] = ["2"];
}
if (Page3.includes(val)) {
data[val] = ["3"];
}
return data;
});
console.log(Page1data);
If I get it right, the problem is with your declaration.
.push() is for arrays not for objects. You have declared your data variable as an object.
You should use:
let data = [];
instead of
let data = {};
So it's going to look like this:
let data = [];
if (Page1.includes(val)) {
data.push("1");
}
etc...

NodeJS TypeError: Cannot read properties of undefined (reading '0')

I get the enemyCards from the frontend and it is an array, with 990 x 7 poker cards.
The sortCardOrder function just take the cards in order so i can search in my datas.
This is my NodeJS code:
import fs from 'fs';
import G from 'generatorics';
export default function findEnemyStrongest(enemyCards) {
let eCombination = enemyCards.enemyCards;
let result = [];
for(const comb of eCombination){
result.push(findStrongest(comb));
}
console.log(result);
}
function createCombinations(enemyC){
let combine = enemyC;
let onlyName = [];
let allCombinations = [];
for (let card of combine){
onlyName.push(card.name);
}
for (let comb of G.combination(onlyName, 5)){
allCombinations.push(comb.slice());
}
return allCombinations;
}
function findStrongest(combi){
let rawdata = fs.readFileSync('data.json');
let strenghtOrder = JSON.parse(rawdata);
let combinations = createCombinations(combi);
let combinationsName = [];
let ordered = "";
let result = [];
for(let combination of combinations){
let nameString = "";
let colors = {'C': 0, 'S': 0, 'H':0, 'D':0};
for(let card of combination){
nameString += card[0];
colors[card[1]]+=1
}
ordered = sortCardOrder(nameString, colors);
combinationsName.push(ordered);
console.log(combinationsName)
result.push(strenghtOrder.cardStrenght[ordered]);
}
return Math.min(...result);
}
function sortCardOrder(string, colors){
}
Can anyone know what is the problem?
We can infer this line is causing the error:
nameString += card[0];
It is requesting the 0 property of the card variable, but card is undefined. Card gets its value from combination. Print out combination to see if it has undefined values.
...
for(let combination of combinations){
console.log(combination)
...
Combination gets it value from combinations, which comes from comb. Print out the values of comb.
...
for (let comb of G.combination(onlyName, 5)){
console.log(comb)
...
Keep going backwards until you find the source of the 'undefined' value. Without seeing the original source data (from G), stepping through the code is the only way to find the error source.

How to parse JSON Date object in NodeJS?

How to parse json date like /Date(1391802454790-0700)/ to (01/31/2014 11:44 AM) in NodeJS.
JSON Date Object:
{
...,
"BirthDate":"\/Date(1391802454790-0700)\/",
...
}
I tried below code but it doesn't work:
var jsondate = "/Date(1391802454790-0700)/";
const data = JSON.parse(jsondate);
console.log(data);
let obj = {
birthday: "/Date(1391802454790-0700)/",
};
let regex = /\(([^)]+)\-\d+/g;
let match = regex.exec(obj.birthday);
if (match) {
console.log(match[1]);
console.log(new Date(Number(match[1]))); // 2014-02-07T19:47:34.790Z - You can use libs to format date
}
uhmm, You just gotta use the value of Birthdate and replace the unwanted characters so you can obtain 1391802454790 part; after that you just need to create a valid date out of this 1391802454790 in the desired format with new Date():
let data = {
...,
"BirthDate":"\/Date(1391802454790-0700)\/",
...
};
let birthdate = data.Birthdate;
birthdate = birthdate.replace("/Date(", "").replace("-0700)/", "");
let formatedBirthdate = new Date(birthdate);

How do I get these properties to not be undefined when they're in an embed

const Discord = require('discord.js')
const prefix1 = '*add'
const prefix2 = '*what'
const prefix3 = '*remove'
const prefix4 = '*search'
const bot4 = new Discord.Client();
let a = []
let fakea = []
bot4.on('message', msg => {
if(msg.member.hasPermission('ADMINISTRATOR')){
if(msg.content.startsWith(prefix1)){
let splited = msg.content.split(' ')
let unchanged = msg.content.split(' ')
splited.splice('*info', 1)
splited.splice(msg.content[1], 1)
splited.splice(msg.content[2], 1)
let c = splited.join(' ')
b = {
namer: unchanged[1],
imformation: unchanged[2],
description: c
}
if(fakea.includes(unchanged[1])){
msg.channel.send('It already exists')
} else {
a.push(b)
fakea.push(unchanged[1])
}
console.log(a)
}
if(msg.content.startsWith(prefix3)){
let armay = msg.content.split(' ')
console.log(a)
console.log(fakea)
if(armay.length != 2){
msg.channel.send(`You have either less or more than two words. That either means you wrote *add on it's own or you had more than one word that you put with the command`)
} else {
if(!fakea.includes(armay[1])){
msg.channel.send(`That doesn't exist. You can't delete something that doesn't exist.`)
} else {
let fakeafind = fakea.find(plot => plot === armay[1])
let afind = a.find(plote => plote.namer === armay[1])
fakea.splice(fakeafind, 1)
a.splice(afind, 1)
}
console.log(a)
console.log(fakea)
}
}
if(msg.content.startsWith(prefix2)){
let coolon = fakea.join('\n')
let don = `_________\n[\n${coolon}\n]\n_________`
const notbot3embed = new Discord.MessageEmbed()
.setTitle('Everything you can search')
.setColor('15DD7C')
.addField('The things you can search', don)
msg.channel.send(notbot3embed)
}
if(msg.content.startsWith(prefix4)){
let mayi = msg.content.split(' ')
if(mayi.length != 2){
msg.channel.send(`You have either less or more than two words. That either means you wrote *search on it's own or you had more than one word that you put with the command`)
} else {
if(fakea.includes(mayi[1])){
let ft = a.filter(thing => thing.namer === mayi[1])
console.log(ft)
let secot = ft.namer
let thirt = ft.imformation
let fort = ft.description
const someembed = new Discord.MessageEmbed()
.setTitle(secot)
.setColor('FF4200')
.addField(thirt, fort)
msg.channel.send(someembed)
} else {
msg.channel.send('This is not a searchable term. Use *what to see the terms that are there.')
}
}
}
}
})
bot4.login(process.env.token4)
I wrote all of it because you would be confused about the properties if I didn't. In the last part I try to get the properties of the object with the name after '*search'. Then I want to put it an embed. Here's the problem I get. On the embed all three of the things say undefined. How do I fix this
If you're confused what I'm trying to do here's what I'm trying to do. I'm trying to make a system that you can put search things(I don't know how to frase it), remove them, check which ones exist and search something. Most of it is working. But in the search part it says for all of them in the embed undefined.
I found out that you need to use find instead of filter. Find works.

Resources