javascript - 删除/添加对象属性
问题描述
我在执行一项简单的任务时遇到困难,我无法弄清楚,如果有人可以提供帮助,我会很高兴。
我有一个具有多个属性的对象,我想过滤掉一些属性。我创建了一个数组,其中包含要从对象中过滤掉的属性。
const str = `
{"id":63,"parent_id":0,"number":"63","order_key":"wc_order_JQR7ZXgFWE4MU","created_via":"admin","version":"3.9.1","status":"pending","currency":"GBP","date_created":"2020-01-30T14:07:52","date_created_gmt":"2020-01-30T14:07:52","date_modified":"2020-01-30T14:08:04","date_modified_gmt":"2020-01-30T14:08:04","discount_total":"0.00","discount_tax":"0.00","shipping_total":"0.00","shipping_tax":"0.00","cart_tax":"0.00","total":"0.00","total_tax":"0.00","prices_include_tax":false,"customer_id":0,"customer_ip_address":"","customer_user_agent":"","customer_note":"","billing":{"first_name":"asfaf","last_name":"asfaf","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"GB","email":"asasfasf@eta.com","phone":"14124"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"","payment_method_title":"","transaction_id":"","date_paid":null,"date_paid_gmt":null,"date_completed":null,"date_completed_gmt":null,"cart_hash":"","meta_data":[],"line_items":[],"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[],"refunds":[],"_links":{"self":[{"href":"https:\/\/example.com\/wp-json\/wc\/v3\/orders\/63"}],"collection":[{"href":"https:\/\/example.com\/wp-json\/wc\/v3\/orders"}]}}
`;
const unwanted = ['id', 'parent_id', 'number', 'order_key', 'created_via', 'version', '_links'];
const hey = JSON.parse(str);
所以我想返回一个没有“不需要的”属性的对象。
我还尝试在该对象内的数组中添加一个新参数。我希望能够将此参数插入到行项目数组中:{ product_id: 123 }。因此订单项应如下所示:
line_items: [
{
product_id: 123
}
]
谢谢!
** 编辑 ** 我发现我可以使用删除方法。不需要的.forEach(i => delete hey[i]);
现在我试图弄清楚如何将对象添加到该对象内的数组中。谢谢!
解决方案
您可以使用filter()
;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
const str = `
{"id":63,"parent_id":0,"number":"63","order_key":"wc_order_JQR7ZXgFWE4MU","created_via":"admin","version":"3.9.1","status":"pending","currency":"GBP","date_created":"2020-01-30T14:07:52","date_created_gmt":"2020-01-30T14:07:52","date_modified":"2020-01-30T14:08:04","date_modified_gmt":"2020-01-30T14:08:04","discount_total":"0.00","discount_tax":"0.00","shipping_total":"0.00","shipping_tax":"0.00","cart_tax":"0.00","total":"0.00","total_tax":"0.00","prices_include_tax":false,"customer_id":0,"customer_ip_address":"","customer_user_agent":"","customer_note":"","billing":{"first_name":"asfaf","last_name":"asfaf","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"GB","email":"asasfasf@eta.com","phone":"14124"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"","payment_method_title":"","transaction_id":"","date_paid":null,"date_paid_gmt":null,"date_completed":null,"date_completed_gmt":null,"cart_hash":"","meta_data":[],"line_items":[],"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[],"refunds":[],"_links":{"self":[{"href":"https:\/\/example.com\/wp-json\/wc\/v3\/orders\/63"}],"collection":[{"href":"https:\/\/example.com\/wp-json\/wc\/v3\/orders"}]}}
`;
const unwanted = ['id', 'parent_id', 'number', 'order_key', 'created_via', 'version', '_links'];
const obj = JSON.parse(str);
const output = Object.keys(obj).reduce((prev, key) => {
if (unwanted.indexOf(key) > -1 ) {
prev[key] = obj[key];
}
return prev;
}, {});
console.log(output);
推荐阅读
- c - 在 C 中初始化联合的动态数组
- powershell - Powershell图表轴数据类型问题
- java - JSON-B REST 有效负载验证
- mysql - 在第一次 SELECT 查询后从第二个表中获取特定列
- json - /ReportServlet.java:[25,52] 错误:com.google.appengine.labs.repackaged.org.json 包不存在
- java - 我无法在 MacOS 上安装 maven
- javascript - 如何使用 Nativescript 和 Vanilla JS 使用 Firebase 数据填充 Listview
- r - R中的动态名称(子集和合并)
- azure - 无法读取存储帐户和密钥 - Azure
- php - 会话数据如何从一个页面传递到另一个页面?