Delete key if all values are empty

Asked

Viewed 51 times

1

I have an array of Javascript objects and wanted to check if certain keys/columns have the value "" (empty). If you delete this key from my Javascript object array, otherwise keep it the same.

Follow a Javascript Object Array as an example: https://drive.google.com/file/d/1gAIRutLHYS1UZwHXaKVqQmv4bRsg3fy1/view?usp=sharing

The database image reflects better what I hope to get as a result. The data is the same as the link above. inserir a descrição da imagem aqui

I did the code below, but it checks line by line and deletes the line if it is empty.

public filterColumns(productConfig: ProductConfig[]) {

        for (let i = 0; i < productConfig.length; i++) {
            for (let j = 1; j <= 6; j++) {
                if (productConfig[i]['Attr_Val_' + j] === "") {
                    delete productConfig[i]['Attr_Val_' + j];
                }
            }
        }
    }

I needed to check that ALL lines are empty before deleting the key/column.

1 answer

0


Make two loops, like you did, but create a separate array by adding the names of empty keys to all objects. Then make two more loops by deleting the key names that were saved in the array.

const productConfig = [
	{
		"id": 1,
		"Country": "Brazil",
		"Tier": "",
		"IBX": "RJ1",
		"PriceList": "BR - Price List",
		"PriceListDescription": "BR - Price List",
		"UOM": "Each",
		"Currency": "BRL",
		"LastPrice": 0.00,
		"AdjustmentType": "Price Override",
		"AdjustmentAmount": 4458.29,
		"EffectiveStartDate": "0000-00-00",
		"EffectiveEndDate": "0000-00-00",
		"Product": "Workstation Seat",
		"PartNumber": "BC00002.ELEM",
		"Attribute_1": "BCTR Room Type",
		"Attr_Val_1": "BCTR-DEDICATED",
		"Operator_1": "",
		"Attribute_2": "Workstation Seat Type",
		"Attr_Val_2": "Back Office",
		"Operator_2": "",
		"Attribute_3": "",
		"Attr_Val_3": "",
		"Operator_3": "",
		"Attribute_4": "",
		"Attr_Val_4": "",
		"Operator_4": "",
		"Attribute_5": "",
		"Attr_Val_5": "",
		"Operator_5": "",
		"Attribute_6": "",
		"Attr_Val_6": "",
		"Operator_6": "",
		"DeleteRule": "",
		"Resale": "",
		"AccountCategory": ""
	},
	{
		"id": 2,
		"Country": "Brazil",
		"Tier": "",
		"IBX": "RJ1",
		"PriceList": "BR - Price List",
		"PriceListDescription": "BR - Price List",
		"UOM": "Each",
		"Currency": "BRL",
		"LastPrice": 0.00,
		"AdjustmentType": "Price Override",
		"AdjustmentAmount": 2229.14,
		"EffectiveStartDate": "0000-00-00",
		"EffectiveEndDate": "0000-00-00",
		"Product": "Workstation Seat - Installation Fee",
		"PartNumber": "BC00002.NR",
		"Attribute_1": "BCTR Room Type",
		"Attr_Val_1": "BCTR-DEDICATED",
		"Operator_1": "",
		"Attribute_2": "Workstation Seat Type",
		"Attr_Val_2": "Back Office",
		"Operator_2": "",
		"Attribute_3": "",
		"Attr_Val_3": "",
		"Operator_3": "",
		"Attribute_4": "",
		"Attr_Val_4": "",
		"Operator_4": "",
		"Attribute_5": "",
		"Attr_Val_5": "",
		"Operator_5": "",
		"Attribute_6": "",
		"Attr_Val_6": "",
		"Operator_6": "",
		"DeleteRule": "",
		"Resale": "",
		"AccountCategory": ""
	},
	{
		"id": 3,
		"Country": "Brazil",
		"Tier": "",
		"IBX": "RJ2",
		"PriceList": "BR - Price List",
		"PriceListDescription": "BR - Price List",
		"UOM": "Each",
		"Currency": "BRL",
		"LastPrice": 0.00,
		"AdjustmentType": "Price Override",
		"AdjustmentAmount": 4293.60,
		"EffectiveStartDate": "0000-00-00",
		"EffectiveEndDate": "0000-00-00",
		"Product": "Workstation Seat",
		"PartNumber": "BC00002.ELEM",
		"Attribute_1": "BCTR Room Type",
		"Attr_Val_1": "BCTR-DEDICATED",
		"Operator_1": "",
		"Attribute_2": "Workstation Seat Type",
		"Attr_Val_2": "Back Office",
		"Operator_2": "",
		"Attribute_3": "",
		"Attr_Val_3": "",
		"Operator_3": "",
		"Attribute_4": "",
		"Attr_Val_4": "",
		"Operator_4": "",
		"Attribute_5": "",
		"Attr_Val_5": "",
		"Operator_5": "",
		"Attribute_6": "",
		"Attr_Val_6": "",
		"Operator_6": "",
		"DeleteRule": "",
		"Resale": "",
		"AccountCategory": ""
	},
	{
		"id": 4,
		"Country": "Brazil",
		"Tier": "",
		"IBX": "RJ2",
		"PriceList": "BR - Price List",
		"PriceListDescription": "BR - Price List",
		"UOM": "Each",
		"Currency": "BRL",
		"LastPrice": 0.00,
		"AdjustmentType": "Price Override",
		"AdjustmentAmount": 2146.80,
		"EffectiveStartDate": "0000-00-00",
		"EffectiveEndDate": "0000-00-00",
		"Product": "Workstation Seat - Installation Fee",
		"PartNumber": "BC00002.NR",
		"Attribute_1": "BCTR Room Type",
		"Attr_Val_1": "BCTR-DEDICATED",
		"Operator_1": "",
		"Attribute_2": "Workstation Seat Type",
		"Attr_Val_2": "Back Office",
		"Operator_2": "",
		"Attribute_3": "",
		"Attr_Val_3": "",
		"Operator_3": "",
		"Attribute_4": "",
		"Attr_Val_4": "",
		"Operator_4": "",
		"Attribute_5": "",
		"Attr_Val_5": "",
		"Operator_5": "",
		"Attribute_6": "",
		"Attr_Val_6": "",
		"Operator_6": "",
		"DeleteRule": "",
		"Resale": "",
		"AccountCategory": ""
	},
	{
		"id": 5,
		"Country": "Brazil",
		"Tier": "",
		"IBX": "SP1",
		"PriceList": "BR - Price List",
		"PriceListDescription": "BR - Price List",
		"UOM": "Each",
		"Currency": "BRL",
		"LastPrice": 0.00,
		"AdjustmentType": "Price Override",
		"AdjustmentAmount": 4376.16,
		"EffectiveStartDate": "0000-00-00",
		"EffectiveEndDate": "0000-00-00",
		"Product": "Workstation Seat",
		"PartNumber": "BC00002.ELEM",
		"Attribute_1": "BCTR Room Type",
		"Attr_Val_1": "BCTR-DEDICATED",
		"Operator_1": "",
		"Attribute_2": "Workstation Seat Type",
		"Attr_Val_2": "Back Office",
		"Operator_2": "",
		"Attribute_3": "",
		"Attr_Val_3": "",
		"Operator_3": "",
		"Attribute_4": "",
		"Attr_Val_4": "",
		"Operator_4": "",
		"Attribute_5": "",
		"Attr_Val_5": "",
		"Operator_5": "",
		"Attribute_6": "",
		"Attr_Val_6": "",
		"Operator_6": "",
		"DeleteRule": "",
		"Resale": "",
		"AccountCategory": ""
	},
	{
		"id": 6,
		"Country": "Brazil",
		"Tier": "",
		"IBX": "SP1",
		"PriceList": "BR - Price List",
		"PriceListDescription": "BR - Price List",
		"UOM": "Each",
		"Currency": "BRL",
		"LastPrice": 0.00,
		"AdjustmentType": "Price Override",
		"AdjustmentAmount": 2188.08,
		"EffectiveStartDate": "0000-00-00",
		"EffectiveEndDate": "0000-00-00",
		"Product": "Workstation Seat - Installation Fee",
		"PartNumber": "BC00002.NR",
		"Attribute_1": "BCTR Room Type",
		"Attr_Val_1": "BCTR-DEDICATED",
		"Operator_1": "",
		"Attribute_2": "Workstation Seat Type",
		"Attr_Val_2": "Back Office",
		"Operator_2": "",
		"Attribute_3": "",
		"Attr_Val_3": "",
		"Operator_3": "",
		"Attribute_4": "",
		"Attr_Val_4": "",
		"Operator_4": "",
		"Attribute_5": "",
		"Attr_Val_5": "",
		"Operator_5": "",
		"Attribute_6": "",
		"Attr_Val_6": "",
		"Operator_6": "",
		"DeleteRule": "",
		"Resale": "",
		"AccountCategory": ""
	}];


let deletar = [];

for (let i = 0; i < productConfig.length; i++) {
   for (let j in productConfig[i]) {
      let x = deletar.indexOf(j);
      if(productConfig[i][j] === "" && !~x){
         deletar.push(j); // adiciona o nome da chave vazia
      }else if(productConfig[i][j] !== "" && ~x){
         // remove da array se tiver valor alguma chave vazia que foi adicionada antes
         deletar.splice(x,1);
      }
   }
}

for (let i = 0; i < productConfig.length; i++) {
   for(let d of deletar) delete productConfig[i][d];
}

console.log("Colunas deletadas", deletar); // chaves vazias
console.log(productConfig); // json tratado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.