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

91 views Asked by At

Im having problem with getting splice to work for my /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. of visits on each 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

2

There are 2 answers

0
Igor Doroshenko On

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))
0
Kunal Mukherjee On

These are the following steps in the solution -

  1. Skip header row by Array.prototype.slice
  2. Split by delimiter |
  3. Count the url key
  4. Accumulate the object based on the url key
  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.
  6. Flatten the structure using Array.prototype.flat to meet your desired result

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