【问题标题】:Remove/add object properties删除/添加对象属性
【发布时间】: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


【解决方案1】:

你可以使用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);

【讨论】:

    【解决方案2】:

    好的,所以我发现了如何实现我想要的 :) 以下是我的发现:

    // remove
    unwanted.forEach(i => delete hey[i]);
    
    // add
    hey.line_items.push({ product_id: 123 });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      相关资源
      最近更新 更多