Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除/添加对象属性_Javascript_Arrays_Object_Filter - Fatal编程技术网

Javascript 删除/添加对象属性

Javascript 删除/添加对象属性,javascript,arrays,object,filter,Javascript,Arrays,Object,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",

我很难完成一项简单的任务,我想不出来,如果有人能帮我,我会很高兴的

我有一个具有多个属性的对象,我想过滤掉一些属性。 我已经创建了一个数组,其中包含要从对象中过滤掉的属性

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
  }
]
谢谢

**编辑** 我发现我可以使用delete方法。 不需要的.forEach(i=>delete-hey[i])

现在我想知道如何将一个对象添加到这个对象内的数组中。谢谢

您可以使用
过滤器()

const str=`
{“id”:63,“父项id”:0,“编号”:“63”,“订单密钥”:“wc_order_JQR7ZXgFWE4MU”,“创建日期”:“管理”,“版本”:“3.9.1”,“状态”:“待定”,“货币”:“GBP”,“创建日期”:“2020-01-30T14:07:52”,“创建日期”:“2020-01-30T14:07:52”,“修改日期”:“2020-01-30T14:08:04”,“修改日期”:“2020-01-30T14:08:04”,“折扣税总额”:“0.00”,“运费税总额”:“0.00”,“运费税”:“0.00”,“运费税”:“0.00”,“总额”:“0.00”,“运费税总额”:“0.00”,“价格含税”:假,“客户id”:0,“客户ip地址”:“客户用户代理”:“客户备注”:“账单”:“{”第一名”:“asfaf”,“姓氏”:“asfaf”,“公司”:“地址1”:“地址2”:“城市”:“州”:“邮政编码”国家“:”GB“,”电子邮件“:”asasfasf@eta.com“,”电话“:”14124“,”运输“:”{”名字“:”姓氏“:”公司“:”地址“:”城市“:”州“:”邮政编码“:”国家“:”,”支付方式“:”支付方式“:”标题“,”交易id“:”支付日期“:”空,“支付日期“:”空,“完成日期“:”空,“购物车散列“,”元数据“:[],”行项目“:[],”税行“:[],”运输行“:[],”费用行“:[],”优惠券行“:[],”退款“:[],”链接“:{”自我“:[{”href:“https:\/\/example.com\/wp-json\/wc\/v3\/orders\/63”,“收集“:[{”href:“https:\/\/\/example.com\/wp\/v3\/orders”}
`;
const Undored=['id'、'parent_id'、'number'、'order_key'、'created_via'、'version'、'链接'];
const obj=JSON.parse(str);
常量输出=Object.keys(obj).reduce((prev,key)=>{
如果(不需要的索引(键)>-1){
上一个[键]=对象[键];
}
返回上一个;
}, {});

console.log(输出);
好的,所以我找到了如何实现我想要的:) 以下是我的发现:

// remove
unwanted.forEach(i => delete hey[i]);

// add
hey.line_items.push({ product_id: 123 });

请发表你为实现这一目标所写的文章,以及你在这方面遇到的具体问题。