#javascript #arrays #object
Вопрос:
var toRemove = [
"amount_after_regular_discount",
"amount_after_to",
"amount_after_activ_disc",
"amount_after_activ_plus_disc",
"total_advance_tax",
"amount_after_special_discount",
"amount_after_discount",
];
var items = [
{
zone: "North",
unique_customers: "90",
total_customers: "90",
orders: "90",
total_of_item_purchased_price: "429432.13",
amount_without_discount: "415632.13",
amount_after_to: "378418.72",
amount_after_activ_disc: "378418.72",
amount_after_activ_plus_disc: "378418.72",
total_gst: "0.00",
total_advance_tax: "0.00",
amount_after_discount: "378418.72",
amount_after_tax: "378418.72",
sale_date: "2021-09-01",
},
{
zone: "North",
unique_customers: "90",
total_customers: "90",
orders: "90",
total_of_item_purchased_price: "429432.13",
amount_without_discount: "415632.13",
amount_after_to: "378418.72",
amount_after_activ_disc: "378418.72",
amount_after_activ_plus_disc: "378418.72",
total_gst: "0.00",
total_advance_tax: "0.00",
amount_after_discount: "378418.72",
amount_after_tax: "378418.72",
sale_date: "2021-09-01",
},
];
Я хочу удалить все ключи, включенные в массив toRemove
, из items
массива объектов. поэтому я хочу получить результирующий массив объектов без всех ключей, включенных в toRemove
массив
Ответ №1:
var toRemove = [
"amount_after_regular_discount",
"amount_after_to",
"amount_after_activ_disc",
"amount_after_activ_plus_disc",
"total_advance_tax",
"amount_after_special_discount",
"amount_after_discount",
];
var items = [
{
zone: "North",
unique_customers: "90",
total_customers: "90",
orders: "90",
total_of_item_purchased_price: "429432.13",
amount_without_discount: "415632.13",
amount_after_to: "378418.72",
amount_after_activ_disc: "378418.72",
amount_after_activ_plus_disc: "378418.72",
total_gst: "0.00",
total_advance_tax: "0.00",
amount_after_discount: "378418.72",
amount_after_tax: "378418.72",
sale_date: "2021-09-01",
},
{
zone: "North",
unique_customers: "90",
total_customers: "90",
orders: "90",
total_of_item_purchased_price: "429432.13",
amount_without_discount: "415632.13",
amount_after_to: "378418.72",
amount_after_activ_disc: "378418.72",
amount_after_activ_plus_disc: "378418.72",
total_gst: "0.00",
total_advance_tax: "0.00",
amount_after_discount: "378418.72",
amount_after_tax: "378418.72",
sale_date: "2021-09-01",
},
];
items.forEach((item) => Object.keys(item).forEach((key) => toRemove.includes(key) amp;amp; delete item[key]))
console.log(items)
Комментарии:
1. Большое Вам спасибо . ты спасла мне день
Ответ №2:
Вы можете сделать это с помощью 2 петель (карта и для, в моем случае) :
const toRemove = [
"amount_after_regular_discount",
"amount_after_to",
"amount_after_activ_disc",
"amount_after_activ_plus_disc",
"total_advance_tax",
"amount_after_special_discount",
"amount_after_discount"
];
const items = [
{
zone: "North",
unique_customers: "90",
total_customers: "90",
orders: "90",
total_of_item_purchased_price: "429432.13",
amount_without_discount: "415632.13",
amount_after_to: "378418.72",
amount_after_activ_disc: "378418.72",
amount_after_activ_plus_disc: "378418.72",
total_gst: "0.00",
total_advance_tax: "0.00",
amount_after_discount: "378418.72",
amount_after_tax: "378418.72",
sale_date: "2021-09-01"
},
{
zone: "North",
unique_customers: "90",
total_customers: "90",
orders: "90",
total_of_item_purchased_price: "429432.13",
amount_without_discount: "415632.13",
amount_after_to: "378418.72",
amount_after_activ_disc: "378418.72",
amount_after_activ_plus_disc: "378418.72",
total_gst: "0.00",
total_advance_tax: "0.00",
amount_after_discount: "378418.72",
amount_after_tax: "378418.72",
sale_date: "2021-09-01"
}
];
const stripped = items.map(x => {
for (let i = 0; i < toRemove.length; i ) {
const key = toRemove[i];
delete x[key]
}
return x
})
console.log(stripped)