javascript - 有没有办法在javascript中合并具有相同键值对的对象数组中的值
问题描述
这是我拥有的 json,我试图合并关于 toolName 的对象
[
{
"data": {
"toolName": "Login",
"data": [
{
"scrapValue": " Find The Job That Fits Your Life",
"toolValueId": "097a5264-1370-fc61-299d-648014d4d5a5",
"toolValueName": "Locate login button (optional)"
}
]
}
},
{
"data": {
"toolName": "Search Website",
"data": [
{
"scrapValue": " By continuing, you agree to our Terms of Use and Privacy Policy. Continue with Facebook orContinue account with EmailPasswordPassword must be at least 8 characters, no spaces Continue with Email",
"toolValueId": "d267005a-61cc-041b-cc35-406f14e81fec",
"toolValueName": "newAttribute"
}
]
}
},
{
"data": {
"toolName": "Capture Attribute",
"data": [
{
"scrapValue": "Explore Glassdoor",
"toolValueId": "e1211bc9-72d9-6f29-5a12-ebb5df7cae90",
"toolValueName": "newAttribute"
}
]
}
},
{
"data": {
"toolName": "Login",
"data": [
{
"scrapValue": "Find the Right Job",
"toolValueId": "7aab0479-39bf-9690-3948-9b52af4d1e51",
"toolValueName": "Locate User Name edit box"
}
]
}
},
{
"data": {
"toolName": "Search Website",
"data": [
{
"scrapValue": "Millions of jobs. Search by what matters to you and find the one thats right for you.",
"toolValueId": "c68ed1b8-c2bf-9ea7-62c0-2e80e7911a53",
"toolValueName": "newAttributehvjh"
}
]
}
},
{
"data": {
"toolName": "Capture Attribute",
"data": [
{
"scrapValue": " or",
"toolValueId": "fd29e018-b8b6-4015-a3ce-121e2d8048af",
"toolValueName": "newAttributehvjh"
}
]
}
},
{
"data": {
"toolName": "Login",
"data": [
{
"scrapValue": " Find The Job That Fits Your Life By continuing, you agree to our Terms of Use and Privacy Policy. Continue with Facebook orContinue account with EmailPasswordPassword must be at least 8 characters, no spaces Continue with Email Are You Hiring? Post Jobs",
"toolValueId": "cd551cd9-a511-b6ce-611a-d0fed61db448",
"toolValueName": "Enter User Name"
}
]
}
},
{
"data": {
"toolName": "Search Website",
"data": [
{
"scrapValue": "Glassdoor",
"toolValueId": "72ce7dfb-9a05-41ce-c882-a489a074c5c9",
"toolValueName": "newAttribute"
}
]
}
},
{
"data": {
"toolName": "Capture Attribute",
"data": [
{
"scrapValue": " Are You Hiring? Post Jobs",
"toolValueId": "9bcc2da4-e244-74ab-c36b-eaf9117bb46f",
"toolValueName": "newAttribute"
}
]
}
}
]
所需的输出是
[
{
"data": {
"toolName": "Login",
"data": [
{
"scrapValue": " Find The Job That Fits Your Life",
"toolValueId": "097a5264-1370-fc61-299d-648014d4d5a5",
"toolValueName": "Locate login button (optional)"
},
{
"scrapValue": "Find the Right Job",
"toolValueId": "7aab0479-39bf-9690-3948-9b52af4d1e51",
"toolValueName": "Locate User Name edit box"
},
{
"scrapValue": " Find The Job That Fits Your Life By continuing, you agree to our Terms of Use and Privacy Policy. Continue with Facebook orContinue account with EmailPasswordPassword must be at least 8 characters, no spaces Continue with Email Are You Hiring? Post Jobs",
"toolValueId": "cd551cd9-a511-b6ce-611a-d0fed61db448",
"toolValueName": "Enter User Name"
}
]
}
},
{
"data": {
"toolName": "Search Website",
"data": [
{
"scrapValue": " By continuing, you agree to our Terms of Use and Privacy Policy. Continue with Facebook orContinue account with EmailPasswordPassword must be at least 8 characters, no spaces Continue with Email",
"toolValueId": "d267005a-61cc-041b-cc35-406f14e81fec",
"toolValueName": "newAttribute"
},
{
"scrapValue": "Millions of jobs. Search by what matters to you and find the one thats right for you.",
"toolValueId": "c68ed1b8-c2bf-9ea7-62c0-2e80e7911a53",
"toolValueName": "newAttributehvjh"
},
{
"scrapValue": "Glassdoor",
"toolValueId": "72ce7dfb-9a05-41ce-c882-a489a074c5c9",
"toolValueName": "newAttribute"
}
]
}
},
{
"data": {
"toolName": "Capture Attribute",
"data": [
{
"scrapValue": "Explore Glassdoor",
"toolValueId": "e1211bc9-72d9-6f29-5a12-ebb5df7cae90",
"toolValueName": "newAttribute"
},
{
"scrapValue": " or",
"toolValueId": "fd29e018-b8b6-4015-a3ce-121e2d8048af",
"toolValueName": "newAttributehvjh"
},
{
"scrapValue": " Are You Hiring? Post Jobs",
"toolValueId": "9bcc2da4-e244-74ab-c36b-eaf9117bb46f",
"toolValueName": "newAttribute"
}
]
}
}
]
我正在尝试将数据数组的对象数组的值相对于上述对象对的 toolName 合并,我尝试了以下方法但没有解决
var result = [...this.scrapingProcedure.reduce((m, o) => {
var key = ['toolName'].map(k => o[k]).join('|');
if (m.has(key)) {
m.get(key).data.push(...o.data);
} else {
m.set(key, Object.assign({}, o, { data: o.data }));
}
return m;
}, new Map).values()];
console.log(result);
它显示错误 ERROR TypeError: Found non-callable @@iterator on this line m.get(key).data.push(...o.data);
任何帮助将不胜感激,在此先感谢
解决方案
使用reduce的简单实现
const data = [
{
"data": {
"toolName": "Login",
"data": [
{
"scrapValue": " Find The Job That Fits Your Life",
"toolValueId": "097a5264-1370-fc61-299d-648014d4d5a5",
"toolValueName": "Locate login button (optional)"
}
]
}
},
{
"data": {
"toolName": "Search Website",
"data": [
{
"scrapValue": " By continuing, you agree to our Terms of Use and Privacy Policy. Continue with Facebook orContinue account with EmailPasswordPassword must be at least 8 characters, no spaces Continue with Email",
"toolValueId": "d267005a-61cc-041b-cc35-406f14e81fec",
"toolValueName": "newAttribute"
}
]
}
},
{
"data": {
"toolName": "Capture Attribute",
"data": [
{
"scrapValue": "Explore Glassdoor",
"toolValueId": "e1211bc9-72d9-6f29-5a12-ebb5df7cae90",
"toolValueName": "newAttribute"
}
]
}
},
{
"data": {
"toolName": "Login",
"data": [
{
"scrapValue": "Find the Right Job",
"toolValueId": "7aab0479-39bf-9690-3948-9b52af4d1e51",
"toolValueName": "Locate User Name edit box"
}
]
}
},
{
"data": {
"toolName": "Search Website",
"data": [
{
"scrapValue": "Millions of jobs. Search by what matters to you and find the one thats right for you.",
"toolValueId": "c68ed1b8-c2bf-9ea7-62c0-2e80e7911a53",
"toolValueName": "newAttributehvjh"
}
]
}
},
{
"data": {
"toolName": "Capture Attribute",
"data": [
{
"scrapValue": " or",
"toolValueId": "fd29e018-b8b6-4015-a3ce-121e2d8048af",
"toolValueName": "newAttributehvjh"
}
]
}
},
{
"data": {
"toolName": "Login",
"data": [
{
"scrapValue": " Find The Job That Fits Your Life By continuing, you agree to our Terms of Use and Privacy Policy. Continue with Facebook orContinue account with EmailPasswordPassword must be at least 8 characters, no spaces Continue with Email Are You Hiring? Post Jobs",
"toolValueId": "cd551cd9-a511-b6ce-611a-d0fed61db448",
"toolValueName": "Enter User Name"
}
]
}
},
{
"data": {
"toolName": "Search Website",
"data": [
{
"scrapValue": "Glassdoor",
"toolValueId": "72ce7dfb-9a05-41ce-c882-a489a074c5c9",
"toolValueName": "newAttribute"
}
]
}
},
{
"data": {
"toolName": "Capture Attribute",
"data": [
{
"scrapValue": " Are You Hiring? Post Jobs",
"toolValueId": "9bcc2da4-e244-74ab-c36b-eaf9117bb46f",
"toolValueName": "newAttribute"
}
]
}
}
]
const res = data.reduce((all, next) => {
const current = all.find(v => v.data.toolName === next.data.toolName)
if (current) {
current.data.data = current.data.data.concat(next.data.data)
} else {
all.push(next)
}
return all
}, [])
console.log(res)
推荐阅读
- node.js - Cheerio.load 与 Google Assistant 的响应相混淆
- swift - 如何在整个游戏中的多个位置添加我的点心节点?
- html - 如何查找/获取文本节点的前兄弟节点?
- dart - 在 Column Flutter 中居中展开 ListView
- javascript - 比较javascript中的函数参数
- java - 如何连续动画 Fab 按钮(放大/缩小)?
- python - 我的 Python 线性回归线不完整
- react-native - 如何在 ios 中的 react native webview 中捕获所有错误事件?
- java - ModelMapper 无法在 DeleteMapping 上将 java.util.list 转换为 java.util.list
- excel - Excel 2016 向 LAST ROW 插入新表行弄乱了公式