我想为一个对象创建一个group-By函数,在该函数中,我按字段对数组进行分组,然后根据它们对应的组对象字段从数组中“强制”即将到来文档的所有字段。 / p>
const groupInDoc = (array, fieldname) => {
let groupedResultDoc = array.reduce((carDocs, current) => {
let keyCount = Object.keys(current).length;
let obj = {};
for (let [key, value] of Object.entries(current)) {
console.log(`${key}: ${value}`);
//check if key is same as the fieldname to group-by.
if (key == fieldname) {
} else {
obj[key] = value;
}
}
if (carDocs.hasOwnProperty(current[fieldname])) {
//if (Array.isArray(carDocs[current[fieldname]])){
carDocs[current[fieldname]] = obj;
//}
} else {
carDocs[current[fieldname]] = obj;
}
return carDocs;
}, Object.create({}));
return groupedResultDoc;
}
我现在有一个问题,如何通过数组对象中的其他对应对象字段扩展分组对象的字段?
例如,如果我的分组对象有一个带有字段数组和字段“字符串”的组键的子文档,那么我想将所有新的数组值从匹配的组对象推入旧数组,并且我也想加上“ +”来强迫字符串...我该怎么做?
编辑:我的原始数据:
let doc = [
{
"car": "Ford",
"prices": ["12", "3", "5", "1"],
"model": "SUV"
},
{
"car": "Ford",
"prices": ["99","88","77"],
"model": "T3"
},
{
"car": "Toyota",
"prices": ["33","44","55"],
"model": "Subaru"
},
{
"car": "Toyota",
"prices": ["66", "50", "22"],
"model": "Cheyenne"
},
{
"car": "Peugeot",
"prices": ["1","2","3"],
"model" : "503"
}
];
我的结果是:
CarDocs: { Ford: { prices: [ '99', '88', '77' ], model: 'T3' },
Toyota: { prices: [ '66', '50', '22' ], model: 'Cheyenne' },
Peugeot: { prices: [ '1', '2', '3' ], model: '503' } }
但应该是:
CarDocs: { Ford: { prices: ["12", "3", "5", "1", '99', '88', '77' ], model: 'T3', 'SUV' },
Toyota: { prices: [33","44","55", '66', '50', '22' ], model: 'Cheyenne', 'Subaru' },
Peugeot: { prices: [ '1', '2', '3' ], model: '503' } }
答案 0 :(得分:2)
您可以根据每个循环上的键名通过reduce合并每个对象
let doc = [{"car": "Ford","prices": ["12", "3", "5", "1"],"model": "SUV"},{"car": "Ford","prices": ["99","88","77"],"model": "T3"},{"car": "Toyota","prices": ["33","44","55"],"model": "Subaru"},{"car": "Toyota","prices": ["66", "50", "22"],"model": "Cheyenne"},{"car": "Peugeot","prices": ["1","2","3"],"model" : "503"}];
let CarDoc = doc.reduce((a, {car, prices, model}) => {
if(a[car]) {
prices.forEach(p => a[car].prices.push(p))
a[car].model = [...a[car].model, model]
} else {
a[car] = {prices, model:[model]}
}
return a
}, {})
console.log(CarDoc)
可读性较低的单行版本:
let doc = [{"car": "Ford","prices": ["12", "3", "5", "1"],"model": "SUV"},{"car": "Ford","prices": ["99","88","77"],"model": "T3"},{"car": "Toyota","prices": ["33","44","55"],"model": "Subaru"},{"car": "Toyota","prices": ["66", "50", "22"],"model": "Cheyenne"},{"car": "Peugeot","prices": ["1","2","3"],"model" : "503"}];
let CarDoc = doc.reduce((a, {car, prices, model}) => a[car] ? {...a, [car]: {prices: a[car].prices.concat(prices), model: a[car].model.concat(model)}} : {...a, [car]: {prices, model:[model]}}, {})
console.log(CarDoc)
编辑:
let doc = [{"car": "Ford", test: 'test', "prices": ["12", "3", "5", "1"],"model": "SUV"},{"car": "Ford", test: 'test', "prices": ["99","88","77"],"model": "T3"},{"car": "Toyota","prices": ["33","44","55"],"model": "Subaru"},{"car": "Toyota","prices": ["66", "50", "22"],"model": "Cheyenne"},{"car": "Peugeot","prices": ["1","2","3"],"model" : "503"}];
let CarDoc = doc.reduce((a, {car, ...rest}) => {
Object.entries(rest).forEach(([k,v]) => {
if(a[car]) {
a[car][k] = [...a[car][k] || [], v]
} else {
a[car] = {...a[car], [k]: [v]}
}
})
return a
}, {})
console.log(CarDoc)
答案 1 :(得分:1)
您需要将值与先前的值合并,而在代码中,您需要在每次迭代中分配新的值
let doc = [{"car": "Ford","prices": ["12", "3", "5", "1"],"model": "SUV"},{"car": "Ford","prices": ["99","88","77"],"model": "T3"},{"car": "Toyota","prices": ["33","44","55"],"model": "Subaru"},{"car": "Toyota","prices": ["66", "50", "22"],"model": "Cheyenne"},{"car": "Peugeot","prices": ["1","2","3"],"model" : "503"}];
let final = doc.reduce((op,{car,prices,model:m})=>{
op[car] = op[car] || {prices:[],model:[]}
op[car].prices = [...op[car].prices, ...prices]
op[car].model = [...op[car].model, m]
return op
},{})
console.log(final)
答案 2 :(得分:0)
无耻的插头。我的库blinq
提供了许多使这种转换变得容易的函数:
const result = blinq(doc)
.groupBy(c => c.car)
.select(g => ({
car: g.key,
prices: g
.selectMany(c => c.prices)
.distinct()
.toArray(),
models: g.select(c => c.model).toArray()
}))
.toArray();
const doc = [{
"car": "Ford",
"prices": ["12", "3", "5", "1"],
"model": "SUV"
},
{
"car": "Ford",
"prices": ["99", "88", "77"],
"model": "T3"
},
{
"car": "Toyota",
"prices": ["33", "44", "55"],
"model": "Subaru"
},
{
"car": "Toyota",
"prices": ["66", "50", "22"],
"model": "Cheyenne"
},
{
"car": "Peugeot",
"prices": ["1", "2", "3"],
"model": "503"
}
];
const {
blinq
} = window.blinq
const result = blinq(doc)
.groupBy(c => c.car)
.select(g => ({
car: g.key,
prices: g
.selectMany(c => c.prices)
.distinct()
.toArray(),
models: g.select(c => c.model).toArray()
}))
.toArray();
console.log(result)
<script src="https://cdn.jsdelivr.net/npm/blinq"></script>