【问题标题】:I want to add an value to an array, trying using splice, but it want work?我想向数组添加一个值,尝试使用拼接,但它需要工作吗?
【发布时间】:2021-01-25 10:24:04
【问题描述】:

我无法让 splice 为我的 /home 工作。

我显然做错了什么,但我不知道是什么,请帮忙?

const logArray = [
  "|timestamp              |url           |userid|",
  "|2019-03-01 09:00:00UTC |/contact.html |12345 |",
  "|2019-03-01 09:00:00UTC |/contact.html |12346 |",
  "|2019-03-01 10:00:00UTC |/contact.html |12345 |",
  "|2019-03-01 10:30:00UTC |/home.html    |12347 |",
  "|2019-03-01 11:00:00UTC |/contact.html |12347 |",
  "|2019-03-02 11:00:00UTC |/contact.html |12348 |",
  "|2019-03-02 12:00:00UTC |/home.html    |12348 |",
  "|2019-03-03 13:00:00UTC |/home.html    |12349 |",
  ""
];

let result = [];

const urls = () => {
  const url = logArray.map((Objects, index, arr) => {
    return Objects.slice(25, 38);
  });
  console.log("url:", url);
  if (url) {
    const contact = url.find((a) => a.includes("contact"));
    result.push(contact);
    console.log(contact);
    //console.log(contact)
  }
  if (url) {
    const home = url.find((a) => a.includes("home"));
    result.splice(3, 0, home);
    console.log(home);
  }
};
urls();
console.log(result);

我计算了每个 url 上的唯一用户 ID,没有。每个网址的访问次数。

所以我把这个数组拿回来了,根本没有拼接?!:

console.log("result:", result); //output ["/contact.html", "/home.html   ", 5, 4, 1, 1]

这是我想要的样子:

// dream scenario: ["/contact.html", 5, 4, "/home.html", 1, 1]

当我从代码中取出拼接功能并在 codepen 中尝试它时,它工作正常......但不是在完整代码中

【问题讨论】:

  • 您的代码不会产生该输出。我将您的代码移到了 sn-p 中,以便它可以运行。 result 中没有存储任何号码。请确保我们可以重现您提出的问题。

标签: javascript arrays splice array-splice


【解决方案1】:

在我看来,这不是解析日志数据的最佳变体,但如果您愿意的话。这就是解决方案



const logArray = [
  "|timestamp              |url           |userid|",
  "|2019-03-01 09:00:00UTC |/contact.html |12345 |",
  "|2019-03-01 09:00:00UTC |/contact.html |12346 |",
  "|2019-03-01 10:00:00UTC |/contact.html |12345 |",
  "|2019-03-01 10:30:00UTC |/home.html    |12347 |",
  "|2019-03-01 11:00:00UTC |/contact.html |12347 |",
  "|2019-03-02 11:00:00UTC |/contact.html |12348 |",
  "|2019-03-02 12:00:00UTC |/home.html    |12348 |",
  "|2019-03-03 13:00:00UTC |/home.html    |12349 |",
  ""
];

function getDataFromLogs(logs) {
  const formattedLogs = logs.slice(1, logs.length - 1)
  const urls = {}

    formattedLogs.map(log => {
    const route = log.slice(25, 38);
    const userID = log.slice(40, 46);
  
    if (urls[route] === undefined) {
            urls[route] = []
    }
    urls[route].push(userID)
  })
  
  const results = [];
  
  Object.keys(urls).map(key => {
    const data = urls[key];
    results.push(key.trim(), data.length, [...new Set(data)].length);
  })
  
  return results;
}
// ["/contact.html", 5, 4, "/home.html", 3, 3]
console.log(getDataFromLogs(logArray))

【讨论】:

    【解决方案2】:

    这些是解决方案中的以下步骤 -

    1. 通过Array.prototype.slice跳过标题行
    2. 按分隔符分割|
    3. 统计url键
    4. 根据url键累加对象
    5. 现在累积后,计算存储在 items.length 中的累积条目数,对于不同的 userId,使用一个集合来计算它。
    6. 使用Array.prototype.flat 展平结构以满足您想要的结果

    const logArray = [
      "|timestamp              |url           |userid|",
      "|2019-03-01 09:00:00UTC |/contact.html |12345 |",
      "|2019-03-01 09:00:00UTC |/contact.html |12346 |",
      "|2019-03-01 10:00:00UTC |/contact.html |12345 |",
      "|2019-03-01 10:30:00UTC |/home.html    |12347 |",
      "|2019-03-01 11:00:00UTC |/contact.html |12347 |",
      "|2019-03-02 11:00:00UTC |/contact.html |12348 |",
      "|2019-03-02 12:00:00UTC |/home.html    |12348 |",
      "|2019-03-03 13:00:00UTC |/home.html    |12349 |",
      ""
    ];
    
    const result = Object.entries(logArray
    .slice(1)
    .reduce((acc, curr) => {
        if (curr) {
            let [,, url, userId] = curr.trim().split('|');
            url = url.trim();
            userId = userId.trim();
            
            acc[url] = (acc[url] || []).concat({
                    url,
                    userId
            });
        }
        
        return acc;
    }, Object.create(null)))
    .reduce((r, c) => {
        const [key, items] = c;
        const distinctItemCount = new Set(items.map(x => x.userId)).size;
        r.push([key, items.length, distinctItemCount]);
        return r;
    }, [])
    .flat();
    
    console.log(result);

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 2014-03-28
      • 2021-01-28
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      相关资源
      最近更新 更多