今天在写前端接口请求代码时,出现接口因收到多余json键值对而导致的异常,故需要前端在发送请求前对发送的对象进行过滤
let object = { a: 5, b: 6, c: 7 };
object = (({ a, c }) => ({ a, c }))(object);
let object = [{ a: 5, b: 6, c: 7 }];
object.forEach(function (item, index, theArray) {
theArray[index] = (({ a, c })=> ({ a, c }))(item)
});
上面的代码用到了js语法中的Object Destructuring(对象解构)
参考:https://stackoverflow.com/a/39333479
如果你的后端语言是Python,则可以使用如下方式在后端对接收到的json进行过滤
object = {'a': 5, 'b': 6, 'c': 7}
object = {key: object[key] for key in ['a', 'c']}
object = [{'a': 5, 'b': 6, 'c': 7}]
object = [{key: item[key] for key in ['a', 'c']} for item in object]