【发布时间】:2020-01-30 15:53:53
【问题描述】:
我在执行一项简单的任务时遇到困难,我想不通,如果有人可以提供帮助,我会很高兴。
我有一个具有多个属性的对象,我想过滤掉一些属性。 我创建了一个数组,其中包含要从对象中过滤掉的属性。
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]);
现在我想弄清楚如何将对象添加到该对象内的数组中。谢谢!
【问题讨论】:
-
请发布您为尝试完成此任务而编写的内容,以及您遇到的具体问题。
标签: javascript arrays object filter