【问题标题】:I have a list of unique id in which i want to implement a counter for each of the id data will be refreshed every 10 mins我有一个唯一 id 列表,我想在其中为每个 id 数据实现一个计数器,每 10 分钟刷新一次
【发布时间】:2020-10-27 02:16:12
【问题描述】:

数据将每 10 分钟刷新一次,并且计数必须相应更改。

const fs = require('fs');
const readfile = require('readfile');

function readFile(file_name) {
    return new Promise(function(resolve, reject) {
        fs.readFile(file_name, (err, data) => {
            if (err) return reject(err);
            return resolve(data);
        });
    });
}

async function execute(file_name) {
    try {
        const file_data = await readFile(file_name);
        const records = JSON.parse(file_data);
        return records.map(single_record => single_record.rdid);
    }
    catch(e) {
         // error handling
    }
}

execute("data.json").then(push => console.log(push));

这是我的 JSON 文件

[

    {"edid":"r7vr8r", "rdid":"86596", "rssi":"15", "batt":"1.35"},
    {"edid":"v8r48e", "rdid":"98525", "rssi":"15", "batt":"1.35"},
    {"edid":"948rcv", "rdid":"12585", "rssi":"15", "batt":"1.35"},
    {"edid":"d39d38", "rdid":"12585", "rssi":"15", "batt":"1.35"},
    {"edid":"f44914", "rdid":"86596", "rssi":"15", "batt":"1.35"},
    {"edid":"r7vr8r", "rdid":"86596", "rssi":"15", "batt":"1.35"},
    {"edid":"v8r48e", "rdid":"98525", "rssi":"15", "batt":"1.35"},
    {"edid":"948rcv", "rdid":"86596", "rssi":"15", "batt":"1.35"},
    {"edid":"r8v9ee", "rdid":"98525", "rssi":"15", "batt":"1.35"},
    {"edid":"d39d38", "rdid":"98525", "rssi":"15", "batt":"1.35"},
    {"edid":"f44914", "rdid":"12585", "rssi":"15", "batt":"1.35"},
    {"edid":"r7vr8r", "rdid":"12585", "rssi":"15", "batt":"1.35"},
    {"edid":"v8r48e", "rdid":"98525", "rssi":"15", "batt":"1.35"},
    {"edid":"948rcv", "rdid":"12585", "rssi":"15", "batt":"1.35"},
    {"edid":"r8v9ee", "rdid":"12585", "rssi":"15", "batt":"1.35"},
    {"edid":"d39d38", "rdid":"98525", "rssi":"15", "batt":"1.35"},
    {"edid":"f44914", "rdid":"12585", "rssi":"15", "batt":"1.35"}
]

这是我当前的输出。

[
  '86596', '98525', '12585',
  '12585', '86596', '86596',
  '98525', '86596', '98525',
  '98525', '12585', '12585',
  '98525', '12585', '12585',
  '98525', '12585'
]

但不是这个,我想要类似的东西:

98525: 6
12585: 7
86596: 4

JSON 文件上的数据将每 10 分钟刷新一次

【问题讨论】:

  • reduce 会让你从循环中构造一个结果

标签: javascript node.js arrays json


【解决方案1】:

这可以解决

使用Map

let counter = new Map()

records.forEach(single_record => {
  const n = parseInt(single_record.rdid)
  if (!counter.has(n)) {
    counter.set(n, 1);
  }
  else {
    counter.set(n, counter.get(n) + 1);
  }
})

counter.forEach((value, key) => {
  console.log(key + ":" + value)
})

使用Object


let counter = {}

records.forEach(single_record => {
  if (!counter.hasOwnProperty(single_record.rdid)) {
    counter[single_record.rdid] = 1;
  }
  else {
    counter[single_record.rdid]++;
  }
})

console.log(counter)

【讨论】:

  • 它说“记录”是未定义的。我还需要添加哪些行才能使其正常工作?
  • 你应该在const records = JSON.parse(file_data);之后运行它
  • 如果是,我是否必须定义“记录”,我该怎么做
  • 在您的execute 函数中,records 已经被定义。
【解决方案2】:

简单地获取您的返回数据:


uniqueCount = [
  '86596', '98525', '12585',
  '12585', '86596', '86596',
  '98525', '86596', '98525',
  '98525', '12585', '12585',
  '98525', '12585', '12585',
  '98525', '12585'
];
var count = {};
uniqueCount.forEach(function(i) { 
   count[i] = (count[i]||0) + 1;
});
console.log(count);

// result will be {12585: 7, 86596: 4, 98525: 6}

【讨论】:

  • 谢谢,此数据将每 10 分钟刷新一次,因此计数将始终不同。知道怎么做吗?
  • 没问题,只需将数据结果存储在全局变量中即可。
  • 有没有办法将 JSON 文件的输出链接到 'uniqueCount' 而不必键入所有值?
  • 你已经这样做了,execute("data.json").then(data => console.log(data));
  • 例如输出是 12344,12343,12433,16543,12565,14543 而不是 12123:3, 45362:7 54630:6
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多