我们如何将多维数组与 JavaScript 或下划线或 lodash 合并
How we can merge multidimensional array with JavaScript or underscore or lodash
我们如何合并和连接此代码的键值(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 });
两个sheet两个数组data.Not能够将这些多个数组合并为一个数组。 jsonData 给出了两个带有上图的数组。需要合并到一个数组 [[array1],[array2]]
您可以使用嵌套 forEach
。最终目标是创建一个累加器对象,其中所有 Product
作为键,输出数组中需要的值作为它的值。创建一个 defaultObj
并将一周中的所有日期作为键,将 0
作为值。在嵌套数组循环中,destructure to get the Product
and the rest 中的 days
键作为一个单独的对象。遍历 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
的值,并且您不希望输出中的值是 0 的 Monday
键。除了使用 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))
您可以使用 lodash 创建带有 _.flow()
的管道函数。该函数将 sub-arrays 展平为单个数组,按 Product
对项目进行分组,然后将组对象映射回数组,并将每个组合并为单个对象:
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>
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))
我们如何合并和连接此代码的键值(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 });
两个sheet两个数组data.Not能够将这些多个数组合并为一个数组。 jsonData 给出了两个带有上图的数组。需要合并到一个数组 [[array1],[array2]]
您可以使用嵌套 forEach
。最终目标是创建一个累加器对象,其中所有 Product
作为键,输出数组中需要的值作为它的值。创建一个 defaultObj
并将一周中的所有日期作为键,将 0
作为值。在嵌套数组循环中,destructure to get the Product
and the rest 中的 days
键作为一个单独的对象。遍历 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
的值,并且您不希望输出中的值是 0 的 Monday
键。除了使用 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))
您可以使用 lodash 创建带有 _.flow()
的管道函数。该函数将 sub-arrays 展平为单个数组,按 Product
对项目进行分组,然后将组对象映射回数组,并将每个组合并为单个对象:
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>
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))