简体   繁体   English

我想向数组添加一个值,尝试使用拼接,但它想要工作吗?

[英]I want to add an value to an array, trying using splice, but it want work?

Im having problem with getting splice to work for my /home.我在让 splice 为我的 /home 工作时遇到问题。

Im clearly doing something wrong but I cant figure out what, please help?我显然做错了什么,但我不知道是什么,请帮忙?

 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);

and I have counted the unique user ids on each url and no.我计算了每个 url 上的唯一用户 ID,没有。 of visits on each url.每个 url 的访问次数。

So I get this array back, no splice at all?::所以我把这个数组拿回来,根本没有拼接?::

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

this is what I would like it to look like:这就是我希望它看起来像的样子:

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

When I take the splice function out of the code and try it in codepen it works fine....but not in the fully code当我从代码中取出拼接 function 并在 codepen 中尝试时,它工作正常....但不是在完整代码中

In my opinion, this isn't the best variant to parse log data but if you want.在我看来,这不是解析日志数据的最佳变体,但如果您愿意的话。 That's solution这就是解决方案



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))

These are the following steps in the solution -这些是解决方案中的以下步骤 -

  1. Skip header row by Array.prototype.slice通过Array.prototype.slice跳过 header 行
  2. Split by delimiter |按分隔符分割|
  3. Count the url key统计 url 键
  4. Accumulate the object based on the url key根据url key累加object
  5. Now post accumulating, count the number of accumulated entries stored in items.length and for distinct userId(s) use a set to count it.现在在累积后,计算存储在items.length中的累积条目数,并为不同的 userId(s) 使用一个集合来计算它。
  6. Flatten the structure using Array.prototype.flat to meet your desired result使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 想要从数组中拼接一个元素 - Want to splice an element from an array 我想拆分一个数组并根据数组内的一些模式拼接它 - I want to split an array and splice it based on some pattern inside the array 我想在 React Native 中来自 API 的数组中添加一个值 - I want to add a value in array coming from API in React Native array.splice() 返回我想要消除的项目而不是数组减去项目 - array.splice() returns the item I want to eliminate rather than the array minus the item 如何将新图像拼接到我要删除的图像所在的同一 position 中的数组中并显示此图像 - How do I splice a new image into an array in the same position that the image that I want to remove is in and to display this image 我想通过使用参数访问数组的值 - i want to access to array's value by using parameter 我想使用JavaScript访问JSON数组中的输入值 - I want to access inputed Value in JSON array using javascript 我想使用jquery将输入值存储在数组中 - I want to store input value in an array using jquery 我想使用 useState 更改数组中对象的值 - I want to change the value of an object in array using useState 想要使用拼接方法从 object 数组中删除动态附加的卡,但循环迭代得到错误的值 - Want to delete the dynamically appended card from object array using splice method but the loop iteration is getting wrong values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM