我想要显示类别(在给定的例子爱好)基于用户的列表。例如,如果一个人有跳舞和唱歌的爱好,那么他/她的详细信息应该在舞蹈类别和唱歌类别中都可见。寻找基于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上显示结果,如:-
跳舞
迪沙
妮莎
鱼
唱歌
迪沙
鱼
发布于 2020-10-02 22:43:45
可以使用Array.reduce和Array.forEach对其进行转换
使用下面的方法,结果对象的每个属性都将是hobby
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));.as-console-wrapper {
max-height: 100% !important;
}
如果您期望最终的对象是一个对象数组,每个对象都有category和users属性,那么下面就是方法。
在这里,除了数组,我还使用了Object.values,以便将用于在Array.reduce中累加categories和users的结果对象转换为数组。
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));.as-console-wrapper {
max-height: 100% !important;
}
发布于 2020-10-03 16:40:29
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)
});
})https://stackoverflow.com/questions/64173217
复制相似问题