首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我想要显示类别(在给定的例子爱好)基于用户的列表。寻找基于javascript的解决方案

我想要显示类别(在给定的例子爱好)基于用户的列表。寻找基于javascript的解决方案
EN

Stack Overflow用户
提问于 2020-10-02 22:24:08
回答 2查看 57关注 0票数 0

我想要显示类别(在给定的例子爱好)基于用户的列表。例如,如果一个人有跳舞和唱歌的爱好,那么他/她的详细信息应该在舞蹈类别和唱歌类别中都可见。寻找基于javascript的解决方案。

代码语言:javascript
运行
复制
let a = [
    {name: disha, age: 28, hobbies: ['dance', 'sing', 'travel']},
    {name: nisha, age: 28, hobbies: ['dance', 'cook', 'travel', 'Play']},
    {name: fisha, age: 28, hobbies: ['dance', 'sing']},
    {name: sisha, age: 28, hobbies: ['donothing']},
    {name: lisha, age: 28, hobbies: ['travel', 'Exploring']}
];

我想在UI上显示结果,如:-

跳舞

迪沙

妮莎

唱歌

迪沙

EN

回答 2

Stack Overflow用户

发布于 2020-10-02 22:43:45

可以使用Array.reduceArray.forEach对其进行转换

使用下面的方法,结果对象的每个属性都将是hobby

代码语言:javascript
运行
复制
let a = [
  { name: 'disha', age: 28, hobbies: ['dance', 'sing', 'travel'] },
  { name: 'nisha', age: 28, hobbies: ['dance', 'cook', 'travel', 'Play'] },
  { name: 'fisha', age: 28, hobbies: ['dance', 'sing'] },
  { name: 'sisha', age: 28, hobbies: ['donothing'] },
  { name: 'lisha', age: 28, hobbies: ['travel', 'Exploring'] },
];

const formatData = data =>
  data.reduce((res, { name, age, ...rest }) => {
    rest.hobbies.forEach(hobby => {
    //Check if the hobby is present in the result object
    //and push the new user to existing `users` array
    //else create a new hobby object with `users` as a property
      if (res[hobby]) {
        res[hobby].users.push({ name, age });
      } else {
        res[hobby] = {
          users: [{ name, age }],
        };
      }
    });
    return res;
  }, {});

console.log(formatData(a));
代码语言:javascript
运行
复制
.as-console-wrapper {
  max-height: 100% !important;
}

如果您期望最终的对象是一个对象数组,每个对象都有categoryusers属性,那么下面就是方法。

在这里,除了数组,我还使用了Object.values,以便将用于在Array.reduce中累加categoriesusers的结果对象转换为数组。

代码语言:javascript
运行
复制
let a = [
  { name: 'disha', age: 28, hobbies: ['dance', 'sing', 'travel'] },
  { name: 'nisha', age: 28, hobbies: ['dance', 'cook', 'travel', 'Play'] },
  { name: 'fisha', age: 28, hobbies: ['dance', 'sing'] },
  { name: 'sisha', age: 28, hobbies: ['donothing'] },
  { name: 'lisha', age: 28, hobbies: ['travel', 'Exploring'] },
];

const formatData = data => {
  const finalRes = data.reduce((res, { name, age, ...rest }) => {
    rest.hobbies.forEach(hobby => {
    //Check if the hobby is present in the result object
    //and push the new user to existing `users` array
    //else create a new hobby object with `category` and `users` as properties
      if (res[hobby]) {
        res[hobby].users.push({ name, age });
      } else {
        res[hobby] = {
          category: hobby,
          users: [{ name, age }],
        };
      }
    });
    return res;
  }, {});

  return Object.values(finalRes);
};

console.log(formatData(a));
代码语言:javascript
运行
复制
.as-console-wrapper {
  max-height: 100% !important;
}

票数 0
EN

Stack Overflow用户

发布于 2020-10-03 16:40:29

代码语言:javascript
运行
复制
let a = [
    {name: "disha", age: 28, hobbies: ['dance', 'sing', 'travel']},
    {name: "nisha", age: 28, hobbies: ['dance', 'cook', 'travel', 'Play']},
    {name: "fisha", age: 28, hobbies: ['dance', 'sing']},
    {name: "sisha", age: 28, hobbies: ['donothing']},
    {name: "lisha", age: 28, hobbies: ['travel', 'Exploring']}
];

/***
     *** For Demo ***
     establishing a target element and appending a child container for the 
     given requirement -- you can use your mechanism
*/

let newNode = document.createElement("div")
newNode.id = 'newCategory'
document.querySelector('#mainbar').appendChild(newNode)

a.forEach(val => {
    val.hobbies.forEach(element => {

        //condition to check if category already exit, if not create one
        if(!document.querySelector(`#${element}`)){
            let newCat = document.createElement('h2')
            newCat.id = element
            newCat.innerText = element.charAt(0).toUpperCase() + element.slice(1)
            document.querySelector('#newCategory').appendChild(newCat)
        }
        // you can add CSS or child of component of your will
        let paraG = document.createElement('p')
        paraG.textContent = val.name
        document.querySelector(`#${element}`).appendChild(paraG)
    });
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64173217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档