简体   繁体   English

在 Javascript 中的数组中将键和值添加到一个 object

[英]Add key and values to an object in an array in Javascript

I would like to add key and values to my objects in resultLog, and I've tried couple of different things but haven't got it to work.我想在 resultLog 中将键和值添加到我的对象中,我已经尝试了几种不同的方法,但没有成功。 (see further down what I would like to achieve). (进一步了解我想要实现的目标)。

This is my output so far:到目前为止,这是我的 output:

I have pushed my array into objects and added keys.....but now Im stuck.我已经将我的数组推入对象并添加了键.....但现在我卡住了。

let result = [5, 4, 1, 1];
let resultLog = [];

for (let i = 0; i < result.length - 1; i += 2) {
  resultLog.push({ pageViews: result[i], visitors: result[i + 1] });
}
console.log(resultLog);
// output [{pageViews: 5, visitors: 4},{ pageViews: 1, visitors: 1}]

I need to add key and value to each object, but how can I do that?我需要为每个 object 添加键和值,但我该怎么做呢? I've had a look at .splice or .unshift but can't get it to work.我看过.splice.unshift ,但无法正常工作。

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

let result = [
  { url: "/contac.html", pageViews: 5, visitors: 4 },
  { url: "/home.html", pageViews: 1, visitors: 1 }
];

console.log(result)
console.table(result);

This is the array that I extracted the number of unique users of each url and number of pageViews.这是我提取每个 url 的唯一用户数和 pageViews 数的数组。

const filtered =[
"|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 |"]

You can try this你可以试试这个

var obj= {array:[]};
for (let i = 0; i < result.length - 1; i += 2) {
  obj.array.push({ pageViews: result[i], visitors: result[i + 1] });
}
const filtered = [
"|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 |"]

1.statistics page views and visitors 1.统计页面浏览量和访问者

 const pageDict = filtered.reduce((prev, cur) => {
    // remove first `|` and last `|`
    const record = cur.replace(/(^\|)|(\|$)/g, '');
    const [time, url, userId] = record.split('|').map((v) => v.trim());
    if (url in prev) {
        prev[url].pageViews++;
        prev[url].visitors.add(userId);
    } else {
        // if url not in prev, init 
        prev[url] = {
            pageViews: 1,
            visitors: new Set([userId]),
        }
    }
    return prev;
}, {});

2.format data 2.格式化数据

  const result = Object.entries(pageDict).map(([key, value]) => {
    const { pageViews, visitors } = value;
    return {
        url: key,
        pageViews: pageViews,
        visitors: visitors.size,
    }
})
console.log(result);
  1. print result is:打印结果为:

     [ { url: '/contact.html', pageViews: 5, visitors: 4 }, { url: '/home.html', pageViews: 1, visitors: 1 }]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM