我们如何合并和连接此代码的键值(javascript或undescore或lodash)。
阵列数据:
[[
{
"Product": "Men's Cut and Sewn",
"Monday": 13,
"Tuesday": 1.29,
"Thursday": 10,
"Friday": 2,
"Saturday": 2.1,
"Sunday": 12
},
{
"Product": "Men's Inner",
"Monday": 10,
"Tuesday": 1.29,
"Wednesday ": 2.6,
"Thursday": 4.9,
"Friday": 2,
"Saturday": 20,
"Sunday": 12
}
],
[
{
"Product": "Men's Cut and Sewn",
"Monday": 10,
"Tuesday": 45,
"Wednesday": 45,
"Thursday": 23,
"Friday": 2,
"Saturday": 3,
"Sunday": 2
},
{
"Product": "Men's Inner",
"Monday": 1,
"Tuesday": 1,
"Thursday": 34,
"Friday": 5,
"Saturday": 34,
"Sunday": 34
}
]]
期望数据:
[{
{
"Product": "Men's Cut and Sewn",
"Monday": 23,
"Tuesday": 2.58,
"Wednesday": 45,
"Thursday": 33,
"Friday": 4,
"Saturday": 5.1,
"Sunday": 14
},
{
"Product": "Men's Inner",
"Monday": 11,
"Tuesday": 2.29,
"Wednesday ": 2.6,
"Thursday": 38.9,
"Friday": 7,
"Saturday": 54,
"Sunday": 46
}
}]
我尝试使用此JavaScript代码
var finalArr = [];
for (var i = 0; i < arr.length - 1; i++) {
if (i < arr.length - 1 && finalArr.length > 0) {
var arr1 = finalArr;
var arr2 = arr[i]
} else {
var arr1 = arr[i];
var arr2 = arr[i + 1];
}
for (let obj1 of arr1) {
var sampleObj = {};
for (let obj2 of arr2) {
if (obj1.Product === obj2.Product) {
sampleObj.Product = obj1.Product;
for (let day in obj1) {
if (day !== 'Product') {
sampleObj[day] = parseInt(obj1[day] + obj2[day]);
}
}
}
}
finalArr.push(sampleObj);
}
}
它可以是n个数组,并且如果未在同一产品中找到某个数组,则也需要添加键(一个数组没有“ Monday”键,但在第二个数组中具有与同一产品的“ Monday”键,则需要加入两个星期一的值。
this.url = ['assets/sample-dataset.xlsx', 'assets/sheet2.xlsx'];
this.jsonData = XLSX.utils.sheet_to_json(worksheet, { raw: true });
两张两个数组的数据。无法将这些多个数组合并为一个数组。 jsonData给出了上面图像的两个数组。需要合并到一个数组[[array1],[array2]]
答案 0 :(得分:0)
您可以使用嵌套的forEach
。最终目标是创建一个所有Product
作为键的累加器对象,并将输出数组中所需的值作为它的值。创建一个defaultObj
,将一周中的所有天作为键,并将0
作为值。在嵌套数组循环中,destructure获得Product
键的days
和rest作为单独的对象。遍历days
的键并更新各自的Product
的值
const arr = [[{Product:"Men's Cut and Sewn",Monday:13,Tuesday:1.29,Thursday:10,Friday:2,Saturday:2.1,Sunday:12},{Product:"Men's Inner",Monday:10,Tuesday:1.29,"Wednesday":2.6,Thursday:4.9,Friday:2,Saturday:20,Sunday:12}],[{Product:"Men's Cut and Sewn",Monday:10,Tuesday:45,Wednesday:45,Thursday:23,Friday:2,Saturday:3,Sunday:2},{Product:"Men's Inner",Monday:1,Tuesday:1,Thursday:34,Friday:5,Saturday:34,Sunday:34}]],
defaultObj = { Monday: 0, Tuesday: 0, Wednesday: 0, Thursday: 0, Friday: 0, Saturday: 0, Sunday: 0 },
group = { };
arr.forEach(inner => {
inner.forEach(({ Product, ...days }) => {
group[Product] = group[Product] || { Product, ...defaultObj };
for (const key in days)
group[Product][key] += days[key];
})
})
console.log(Object.values(group))
这就是group
对象的样子
{
"Men's Cut and Sewn": {
"Product": "Men's Cut and Sewn",
"Monday": 23,
"Tuesday": 46.29,
"Wednesday": 45,
"Thursday": 33,
"Friday": 4,
"Saturday": 5.1,
"Sunday": 14
},
"Men's Inner": {
"Product": "Men's Inner",
"Monday": 11,
"Tuesday": 2.29,
"Wednesday": 2.6,
"Thursday": 38.9,
"Friday": 7,
"Saturday": 54,
"Sunday": 46
}
}
您可以使用Object.values()
将该对象的值作为数组获取
上面的代码将为一周中的所有日期添加一个值。假设Men's Cut and Sewn
产品在两个数组中都没有Monday
的值,并且您不希望Monday
键在输出中的值为0。除了使用defaultObj
之外,您还可以添加其他if-else
来检查是否已经添加了日期。
const arr = [[{Product:"Men's Cut and Sewn",Monday:13,Tuesday:1.29,Thursday:10,Friday:2,Saturday:2.1,Sunday:12},{Product:"Men's Inner",Monday:10,Tuesday:1.29,"Wednesday":2.6,Thursday:4.9,Friday:2,Saturday:20,Sunday:12}],[{Product:"Men's Cut and Sewn",Monday:10,Tuesday:45,Wednesday:45,Thursday:23,Friday:2,Saturday:3,Sunday:2},{Product:"Men's Inner",Monday:1,Tuesday:1,Thursday:34,Friday:5,Saturday:34,Sunday:34}]],
group = { };
arr.forEach(inner => {
inner.forEach(({ Product, ...days }) => {
group[Product] = group[Product] || { Product };
for (const key in days) {
if(group[Product][key])
group[Product][key] += days[key];
else
group[Product][key] = days[key];
}
})
})
console.log(Object.values(group))
答案 1 :(得分:0)
您可以使用lodash通过_.flow()
创建管道函数。该函数将子数组展平为单个数组,按Product
对项目进行分组,然后将groups对象映射回array,并将每个组合并为单个对象:
const { flow, flatten, partialRight: pr, groupBy, map, mergeWith, isNumber } = _ // replace with imports in your code
const fn = flow(
flatten, // convert to a single array,
pr(groupBy, 'Product'), // convert to groups by with the product as key
pr(map, group => mergeWith({}, ...group, (a, b) => _.isNumber(a) ? _.add(a, b) : undefined)) // combine the items of each group to a single item
)
const arr = [[{Product:"Men's Cut and Sewn",Monday:13,Tuesday:1.29,Thursday:10,Friday:2,Saturday:2.1,Sunday:12},{Product:"Men's Inner",Monday:10,Tuesday:1.29,"Wednesday":2.6,Thursday:4.9,Friday:2,Saturday:20,Sunday:12}],[{Product:"Men's Cut and Sewn",Monday:10,Tuesday:45,Wednesday:45,Thursday:23,Friday:2,Saturday:3,Sunday:2},{Product:"Men's Inner",Monday:1,Tuesday:1,Thursday:34,Friday:5,Saturday:34,Sunday:34}]]
const result = fn(arr)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.js"></script>
以及使用lodash / fp的更短版本:
const { flow, flatten, groupBy, map, mergeAllWith, isNumber } = _ // replace with imports in your code
const fn = flow(
flatten, // convert to a single array,
groupBy('Product'), // convert to groups by with the product as key
map(mergeAllWith((a, b) => _.isNumber(a) ? _.add(a, b) : undefined)) // combine the items of each group to a single item
)
const arr = [[{Product:"Men's Cut and Sewn",Monday:13,Tuesday:1.29,Thursday:10,Friday:2,Saturday:2.1,Sunday:12},{Product:"Men's Inner",Monday:10,Tuesday:1.29,"Wednesday":2.6,Thursday:4.9,Friday:2,Saturday:20,Sunday:12}],[{Product:"Men's Cut and Sewn",Monday:10,Tuesday:45,Wednesday:45,Thursday:23,Friday:2,Saturday:3,Sunday:2},{Product:"Men's Inner",Monday:1,Tuesday:1,Thursday:34,Friday:5,Saturday:34,Sunday:34}]]
const result = fn(arr)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
答案 2 :(得分:0)
const arr = [[{Product:"Men's Cut and Sewn",Monday:13,Tuesday:1.29,Thursday:10,Friday:2,Saturday:2.1,Sunday:12},{Product:"Men's Inner",Monday:10,Tuesday:1.29,"Wednesday":2.6,Thursday:4.9,Friday:2,Saturday:20,Sunday:12}],[{Product:"Men's Cut and Sewn",Monday:10,Tuesday:45,Wednesday:45,Thursday:23,Friday:2,Saturday:3,Sunday:2},{Product:"Men's Inner",Monday:1,Tuesday:1,Thursday:34,Friday:5,Saturday:34,Sunday:34}]],
group = { };
arr.forEach(inner => {
inner.forEach(({ Product, ...days }) => {
group[Product] = group[Product] || { Product };
for (const key in days) {
if(group[Product][key])
group[Product][key] += days[key];
else
group[Product][key] = days[key];
}
})
})
console.log(Object.values(group))