WE_USE_JS Telegram 4945
👩‍💻 Задачка по 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
1



tgoop.com/we_use_js/4945
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/4945

View MORE
Open in Telegram


Telegram News

Date: |

Developing social channels based on exchanging a single message isn’t exactly new, of course. Back in 2014, the “Yo” app was launched with the sole purpose of enabling users to send each other the greeting “Yo.” Among the requests, the Brazilian electoral Court wanted to know if they could obtain data on the origins of malicious content posted on the platform. According to the TSE, this would enable the authorities to track false content and identify the user responsible for publishing it in the first place. The creator of the channel becomes its administrator by default. If you need help managing your channel, you can add more administrators from your subscriber base. You can provide each admin with limited or full rights to manage the channel. For example, you can allow an administrator to publish and edit content while withholding the right to add new subscribers. How to Create a Private or Public Channel on Telegram? 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.
from us


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