显示每个标签出现的次数

Display a count of how many times each tag shows up

我有一个用户对象,我正在尝试显示每个标签出现的次数。 我在 ExaltedToast 代码中编辑了

我期望的输出是:

问题:1

类型:1

结果:1

输出:1

occaecat: 2

..等等

我得到的输出是:

{"quis":1,"wrong":1,"consequent":1,"out":1,"caused":2,"esse":1,"who":1,"in ":1,"duis":1,"officia":1,"ipsum":1,"incididunt":2,"ex":1}

我的数组对象

const users = [
  {
    age: 21,
    eyeColor: "green",
    name: "Gregory Villarreal",
    company: "BLUPLANET",
    tags: ["quis", "irure", "consequat", "ut", "occaecat"],
  },
  {
    age: 39,
    eyeColor: "brown",
    name: "Bowman Jensen",
    company: "HOMETOWN",
    tags: ["esse", "qui", "in", "duis", "occaecat"],
  },
  {
    age: 35,
    eyeColor: "brown",
    name: "Berg Carson",
    company: "WRAPTURE",
    tags: ["officia", "ipsum", "incididunt", "incididunt", "ex"],
  },
];

到目前为止我的代码

// OLD CODE
// get the tags from the array
const userTags = users.map((user) => (user.tags));

// count the tag occurrences
const tagCount = userTags.reduce((obj, e) => {
    obj[e] = (obj[e] || 0) + 1;
    return obj;
  }, {});

// display the tag occurrences
let div = document.createElement('div');
div.innerHTML = JSON.stringify(tagCount);
document.body.appendChild(div);

//  CURRENT CODE
users.forEach((user) => {
    user.tags.forEach((tag) => {
        tagCount[tag] = (tagCount[tag] | 0) + 1;
    });
});

let div = document.createElement('div');
div.innerHTML = JSON.stringify(tagCount);
document.body.appendChild(div);

我有一个 JSBIN https://jsbin.com/leqeludahi/ 和一个代码片段

const users = [
  {
    age: 21,
    eyeColor: "green",
    name: "Gregory Villarreal",
    company: "BLUPLANET",
    tags: ["quis", "irure", "consequat", "ut", "occaecat"],
  },
  {
    age: 39,
    eyeColor: "brown",
    name: "Bowman Jensen",
    company: "HOMETOWN",
    tags: ["esse", "qui", "in", "duis", "occaecat"],
  },
  {
    age: 35,
    eyeColor: "brown",
    name: "Berg Carson",
    company: "WRAPTURE",
    tags: ["officia", "ipsum", "incididunt", "incididunt", "ex"],
  },
];

const tagCount = {};
users.forEach((user) => {
    user.tags.forEach((tag) => {
        tagCount[tag] = (tagCount[tag] | 0) + 1;
    });
});

let div = document.createElement('div');
div.innerHTML = JSON.stringify(tagCount);
// div.innerHTML = tagCount.join('\n');
document.body.appendChild(div);

你很接近,你只需要额外遍历数组中的标签,而不是直接将数组添加到对象中。

// Get the tags from the array
const userTags = users.map((user) => user.tags);

// Count the tag occurrences
const tagCount = userTags.reduce((obj, e) => {
    for (let tag of e) {
        obj[tag] = (obj[tag] || 0) + 1;
    }
    return obj;
}, {});

一个相当简洁的解决方案是在 forEach.

中使用单轮迭代
let tagCount = {};
users.forEach((user) => {
    user.tags.forEach((tag) => {
        tagCount[tag] = (tagCount[tag] | 0) + 1;
    });
});
const tagCount = {};
let div = document.createElement('div');
users.forEach((user) => {
    user.tags.forEach((tag) => {
        tagCount[tag] = (tagCount[tag] | 0) + 1;     
    });

let tagFormat = Object.entries(tagCount).reduce((result, [key, value]) => {
return result + `${key}:${value} <br>`;
}, "");

div.innerHTML = tagFormat;
});

document.body.appendChild(div);