将 JSON 保存到变量中,排除 JSON 的某些元素
Save JSON into a variable, excluding certain elements of the JSON
我正在开发电报 anti-flood 机器人,所以我创建了一个简单的机器人,在发送消息时将文本和发件人 ID 保存在 JSON 中,当发送另一条消息时, 检查两者是否相同,如果相同,则删除该消息;但是该方法有时不起作用并删除了不同的消息(例如:用户快速发送了 2 张没有标题的不同图像,第二张图像将被删除),所以我想保存所有 JSON 不包括特定元素(如消息 ID 和日期),因为它们总是不同的,所以我该怎么做?
这是一个 JSON 消息的例子:
{
message_id: 0,
from: {
id: 0,
is_bot: false,
first_name: 'John',
last_name: 'Doe',
username: 'JohnDoe',
language_code: 'en-us'
},
chat: { id: 0, title: 'Cool Group.', type: 'group' },
date: 12345,
text: 'Test Message.'
}
首先将您的对象存储在某个变量中,然后删除要排除的键值。
var x = {
message_id: 0,
from: {
id: 0,
is_bot: false,
first_name: 'John',
last_name: 'Doe',
username: 'JohnDoe',
language_code: 'en-us'
},
chat: { id: 0, title: 'Cool Group.', type: 'group' },
date: 12345,
text: 'Test Message.'
};
delete x["message_id"]
delete x["date"]
这可能有点矫枉过正,但您可以使用后备默认值创建您期望的结构,然后合并到传入对象中,忽略不在架构中或不同类型的任何属性...称之为严格合并.
function strictMerge(a, b) {
if (!b || typeof b !== typeof a) return a
if (Array.isArray(a) && Array.isArray(b)) {
return a.map((item, index) => {
if (b[index]) {
return Object.keys(item).reduce((acc, key) => {
acc[key] = typeof b[index][key] === 'object' && !Array.isArray(b[key]) ?
this.strictMerge(a[index][key], b[index][key]) : (
typeof b[index][key] !== 'undefined' ? (
typeof b[index][key] !== typeof a[index][key] ? a[index][key] : b[index][key]
) : a[index][key]
);
return acc
}, {})
}
return item
}, [])
} else {
return Object.keys(a).reduce((acc, key) => {
acc[key] = typeof b[key] === 'object' && !Array.isArray(b[key]) ?
strictMerge(a[key], b[key]) : (
typeof b[key] !== 'undefined' ? (
typeof b[key] !== typeof a[key] ? a[key] : b[key]
) : a[key]
)
return acc
}, {})
}
}
const message = {
message_id: 0,
from: {
id: 0,
is_bot: false,
first_name: 'John',
last_name: 'Doe',
username: 'JohnDoe',
language_code: 'en-us'
},
chat: {
id: 0,
title: 'Cool Group.',
type: 'group'
},
date: 12345,
text: 'Test Message.',
foooo: 'barrr'
}
console.log(strictMerge({
from: {
id: 0,
is_bot: false,
first_name: '',
last_name: '',
username: '',
language_code: ''
},
chat: {
id: 0,
title: '',
type: ''
},
text: ''
}, message))
我正在开发电报 anti-flood 机器人,所以我创建了一个简单的机器人,在发送消息时将文本和发件人 ID 保存在 JSON 中,当发送另一条消息时, 检查两者是否相同,如果相同,则删除该消息;但是该方法有时不起作用并删除了不同的消息(例如:用户快速发送了 2 张没有标题的不同图像,第二张图像将被删除),所以我想保存所有 JSON 不包括特定元素(如消息 ID 和日期),因为它们总是不同的,所以我该怎么做? 这是一个 JSON 消息的例子:
{
message_id: 0,
from: {
id: 0,
is_bot: false,
first_name: 'John',
last_name: 'Doe',
username: 'JohnDoe',
language_code: 'en-us'
},
chat: { id: 0, title: 'Cool Group.', type: 'group' },
date: 12345,
text: 'Test Message.'
}
首先将您的对象存储在某个变量中,然后删除要排除的键值。
var x = {
message_id: 0,
from: {
id: 0,
is_bot: false,
first_name: 'John',
last_name: 'Doe',
username: 'JohnDoe',
language_code: 'en-us'
},
chat: { id: 0, title: 'Cool Group.', type: 'group' },
date: 12345,
text: 'Test Message.'
};
delete x["message_id"]
delete x["date"]
这可能有点矫枉过正,但您可以使用后备默认值创建您期望的结构,然后合并到传入对象中,忽略不在架构中或不同类型的任何属性...称之为严格合并.
function strictMerge(a, b) {
if (!b || typeof b !== typeof a) return a
if (Array.isArray(a) && Array.isArray(b)) {
return a.map((item, index) => {
if (b[index]) {
return Object.keys(item).reduce((acc, key) => {
acc[key] = typeof b[index][key] === 'object' && !Array.isArray(b[key]) ?
this.strictMerge(a[index][key], b[index][key]) : (
typeof b[index][key] !== 'undefined' ? (
typeof b[index][key] !== typeof a[index][key] ? a[index][key] : b[index][key]
) : a[index][key]
);
return acc
}, {})
}
return item
}, [])
} else {
return Object.keys(a).reduce((acc, key) => {
acc[key] = typeof b[key] === 'object' && !Array.isArray(b[key]) ?
strictMerge(a[key], b[key]) : (
typeof b[key] !== 'undefined' ? (
typeof b[key] !== typeof a[key] ? a[key] : b[key]
) : a[key]
)
return acc
}, {})
}
}
const message = {
message_id: 0,
from: {
id: 0,
is_bot: false,
first_name: 'John',
last_name: 'Doe',
username: 'JohnDoe',
language_code: 'en-us'
},
chat: {
id: 0,
title: 'Cool Group.',
type: 'group'
},
date: 12345,
text: 'Test Message.',
foooo: 'barrr'
}
console.log(strictMerge({
from: {
id: 0,
is_bot: false,
first_name: '',
last_name: '',
username: '',
language_code: ''
},
chat: {
id: 0,
title: '',
type: ''
},
text: ''
}, message))