【发布时间】:2017-10-19 20:57:58
【问题描述】:
我有一个DOM 树的JSON 版本,我只想保留页面之间的差异(删除导航页脚...)
例子:
const a = {
id: '1',
child: [
{
id: '2',
child: [{id: '1'}, {id: '2'}]
},
{
id: '3',
child: [{id: '1'}, {id: '5'}]
}
]
};
和
const b = {
id: '1',
child: [
{
id: '2',
child: [{id: '1'}, {id: '4'}]
},
{
id: '3',
child: [{id: '1'}, {id: '4'}]
}
]
};
有一个功能
diff(a, b)
这个结果
{
id: '1',
child: [
{
id: '2',
child: [{id: '2'}]
},
{
id: '3',
child: [{id: '5'}]
}
]
}
我基于递归函数创建了这个
const diff = (a, b) => {
if (Array.isArray(a)) {
}
if (typeof a === 'object') {
// ...
extract(a.child, b.child);
}
}
我该怎么做?有npm 包吗?或JSON 路径?我想创建一个函数,删除两个 JSON 文件之间的相等 'parts',函数的输出具有相同的结构,但没有 'equal parts ' 唯一的区别。
【问题讨论】:
-
那不是 JSON。 JSON 是一种用于数据交换的文本符号。 (More here.) 如果您正在处理 JavaScript 源代码,而不是处理 string,那么您就不是在处理 JSON。
标签: javascript json node.js web-crawler