如何解决有没有一种方法可以从JSON中删除但保留其内容?
我有一个这样的JSon:
[
{
"order_number": "","products": [
{
"line_id": "","cod_anf": "","static_field_2": "","nome_armazem": "","cod_armazem": "","gln": "","sku_ah": "","name": "","qty": "","_regular_price": "","_sale_price": "","line_total": "","desconto": "","pva": "","category": "","localidade": " ","codigo_ah": " ","Part of": ""
},{
"line_id": "",}
],{
"order_number": "","products": [
{
"line_id": 1,
我需要删除“产品”,但要保留其内容。
所以输出应该是这样的:
有可能吗?我已经尝试过,但无法正常工作。
谢谢
解决方法
您可以使用嵌套地图,散布运算符和如下的flat
函数。
const json = [{
"order_number": "2","products": [{
"line_id": "","cod_anf": "","static_field_2": "","nome_armazem": "","cod_armazem": "","gln": "","sku_ah": "","name": "","qty": "","_regular_price": "","_sale_price": "","line_total": "","desconto": "","pva": "","category": "","localidade": " ","codigo_ah": " ","Part of": ""
},{
"line_id": "",}
]
},{
"order_number": "1","products": [{
"line_id": 1,}
]
}
];
const flattened = json.map(r => [...r.products.map(q =>
({
order_number: r.order_number,...q
})
)])
.flat(1);
console.log(flattened);
,
尝试一下,为数据分配json
let obj = [];
data.forEach(order =>{
order.products.forEach(product => {
obj.push({
...product,order_number: order.order_number
})
})
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。