我有以下数据:

const data = [
    {
        parent: {
            id: "1",
            name: "Europe"
        },
        item: {
            name: "Italy"
        },
        score: 5
    },
    {
        parent: {
            id: "1",
            name: "Europe"
        },
        item: {
            name: "France"
        },
        score: 4.5
    },
    {
        parent: {
            id: "1",
            name: "Europe"
        },
        item: {
            name: "UK"
        },
        score: 4.9
    },
    {
        parent: {
            id: "2",
            name: "Afrique"
        },
        item: {
            name: "Morocco"
        },
        score: 3.1
    },
    {
        parent: {
            id: "2",
            name: "Afrique"
        },
        item: {
            name: "Egypt"
        },
        score: 3.9
    }
];


我想基于parent.id将其分组并计算平均分数,因此我可以得到以下结果:

[
    {
        parent: {
            id: "1",
            name: "Europe",
            items: [
                {
                    name: "Italy"
                },
                {
                    name: "France"
                },
                {
                    name: "UK"
                }
            ],
            score: 4.8
        }
    },
    {
        parent: {
            id: "2",
            name: "Afrique",
            items: [
                {
                    name: "Morocco"
                },
                {
                    name: "Egypt"
                }
            ],
            score: 3.5
        }
    }
]


我使用了以下函数,但它不适用于嵌套键,并且也不会返回所需的结果模式。

let group = cars.reduce((r, a) => {
   console.log("a", a);
   console.log('r', r);
   r[a.make] = [...r[a.parent.id] || [], a];
   return r;
}, {});
console.log("group", group);

最佳答案

您可以使用_reduce()函数:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

var result = data.reduce((res, data) => {
  if(!res[data.parent.id]) {
    data.item = [data.item];
    res[data.parent.id] = data;
  } else {
    res[data.parent.id]['item'].push(data['item']);
    res[data.parent.id]['score'] = (res[data.parent.id]['score'] + data['score'])/2;
  }
  return res;
}, [])
.filter(x => x != null)




const data = [
    {
        parent: {
            id: "1",
            name: "Europe"
        },
        item: {
            name: "Italy"
        },
        score: 5
    },
    {
        parent: {
            id: "1",
            name: "Europe"
        },
        item: {
            name: "France"
        },
        score: 4.5
    },
    {
        parent: {
            id: "1",
            name: "Europe"
        },
        item: {
            name: "UK"
        },
        score: 4.9
    },
    {
        parent: {
            id: "2",
            name: "Afrique"
        },
        item: {
            name: "Morocco"
        },
        score: 3.1
    },
    {
        parent: {
            id: "2",
            name: "Afrique"
        },
        item: {
            name: "Egypt"
        },
        score: 3.9
    }
];

var result = data.reduce((res, data) => {
  if(!res[data.parent.id]) {
    data.item = [data.item];
    res[data.parent.id] = data;
  } else {
res[data.parent.id]['item'].push(data['item']);
res[data.parent.id]['score'] = (res[data.parent.id]['score'] + data['score'])/2;
  }
  return res;
}, [])
.filter(x => x != null)

console.log(result)

关于javascript - 按嵌套键按对象数组分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60960874/

10-12 03:42