Notice: file_put_contents(): Write of 1047 bytes failed with errno=28 No space left on device in /var/www/tgoop/post.php on line 50

Warning: file_put_contents(): Only 16384 of 17431 bytes written, possibly out of free disk space in /var/www/tgoop/post.php on line 50
Node.JS [ru] | Серверный JavaScript@we_use_js P.4230
WE_USE_JS Telegram 4230
👩‍💻 Задачка по NodeJS

Напишите скрипт на Node.js, который считывает JSON-файл, фильтрует данные по указанному ключу и значению, и выводит результат в консоль.

➡️ Пример:

node filterJson.js data.json age 30

[
{ "name": "Bob", "age": 30 },
{ "name": "Charlie", "age": 30 }
]


Решение задачи ⬇️

const fs = require('fs');

const [,, filePath, key, value] = process.argv;

if (!filePath || !key || !value) {
console.error('Использование: node filterJson.js <filePath> <key> <value>');
process.exit(1);
}

fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Ошибка чтения файла:', err.message);
process.exit(1);
}

try {
const jsonData = JSON.parse(data);
const filteredData = jsonData.filter(item => item[key] == value);
console.log(JSON.stringify(filteredData, null, 2));
} catch (parseErr) {
console.error('Ошибка парсинга JSON:', parseErr.message);
process.exit(1);
}
});
Please open Telegram to view this post
VIEW IN TELEGRAM
2👍1👎1👏1



tgoop.com/we_use_js/4230
Create:
Last Update:

👩‍💻 Задачка по NodeJS

Напишите скрипт на Node.js, который считывает JSON-файл, фильтрует данные по указанному ключу и значению, и выводит результат в консоль.

➡️ Пример:

node filterJson.js data.json age 30

[
{ "name": "Bob", "age": 30 },
{ "name": "Charlie", "age": 30 }
]


Решение задачи ⬇️

const fs = require('fs');

const [,, filePath, key, value] = process.argv;

if (!filePath || !key || !value) {
console.error('Использование: node filterJson.js <filePath> <key> <value>');
process.exit(1);
}

fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Ошибка чтения файла:', err.message);
process.exit(1);
}

try {
const jsonData = JSON.parse(data);
const filteredData = jsonData.filter(item => item[key] == value);
console.log(JSON.stringify(filteredData, null, 2));
} catch (parseErr) {
console.error('Ошибка парсинга JSON:', parseErr.message);
process.exit(1);
}
});

BY Node.JS [ru] | Серверный JavaScript


Share with your friend now:
tgoop.com/we_use_js/4230

View MORE
Open in Telegram


Telegram News

Date: |

In the “Bear Market Screaming Therapy Group” on Telegram, members are only allowed to post voice notes of themselves screaming. Anything else will result in an instant ban from the group, which currently has about 75 members. Find your optimal posting schedule and stick to it. The peak posting times include 8 am, 6 pm, and 8 pm on social media. Try to publish serious stuff in the morning and leave less demanding content later in the day. Public channels are public to the internet, regardless of whether or not they are subscribed. A public channel is displayed in search results and has a short address (link). With the administration mulling over limiting access to doxxing groups, a prominent Telegram doxxing group apparently went on a "revenge spree." The channel also called on people to turn out for illegal assemblies and listed the things that participants should bring along with them, showing prior planning was in the works for riots. The messages also incited people to hurl toxic gas bombs at police and MTR stations, he added.
from us


Telegram Node.JS [ru] | Серверный JavaScript
FROM American