Home > Mobile >  Dynamically delete multiple columns from array
Dynamically delete multiple columns from array

Time:12-21

I am trying to figure out how to delete multiple column from an array of objects based on a dynamically selected list of values.

If I want to delete a specific column, I can do something like below.

Assuming array is:

list = [ 
{ 
  "THEDATE": "12/11/2022",
  "E38": 247, 
  "E40": 212, 
  "N45": 139, 
  "N48:: 10 
}, 
{ 
  "THEDATE": "12/10/2022",
  "E38": 47, 
  "E40": 22, 
  "N45": 19, 
  "N48:: 66 
}, 
{ 
  "THEDATE": "12/12/2022",
  "E38": 24, 
  "E40": 21, 
  "N45": 39, 
  "N48": 34 
}, 
]

If I want to remove "N45", I can use:

let new_list = list.map(function (obj) {
    return {
        "THEDATE": obj.TheDate,
        "E38": obj.e38,
        "E40": obj.e40,
        "N48": obj.n48
    }
}

But if I have a list of column to remove, how would I modify the code?

Assuming

var colToRemove = ["N45", "E38"];

Update - Attempting IE 11 Version

I tried to convert the arrow function to a regular function that IE 11 can understand but I get "colToRemove.has is not a function".

var colToRemove = $(allErrCodes).not(selErrCodes).get();

const altered = list.map(
    function (item) {
        blah = Object.keys(item).reduce(function (acc, key) {
            if (!colToRemove.has(key)) acc[key] = item[key]; return acc;
        }, {})
    }
);

CodePudding user response:

You could reduce the valid keys for each item into a new object.

const main = () => {
  const altered = list.map(item =>
    Object.keys(item).reduce((acc, key) => {
      if (!colToRemove.has(key)) acc[key] = item[key];
      return acc;
    }, {}));

  console.log(altered);
};

const colToRemove = new Set(["N45", "E38"]);

const list = [{
  "THEDATE": "12/11/2022",
  "E38": 247,
  "E40": 212,
  "N45": 139,
  "N48": 10
}, {
  "THEDATE": "12/10/2022",
  "E38": 47,
  "E40": 22,
  "N45": 19,
  "N48": 66
}, {
  "THEDATE": "12/12/2022",
  "E38": 24,
  "E40": 21,
  "N45": 39,
  "N48": 34
}];

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }

If you want to modify the items in-place, you can delete:

Note: Be aware that this mutates the object, rather than creating a copy

const main = () => {
  list.forEach(item =>
    Object.keys(item).forEach(key => {
      if (colToRemove.has(key)) {
        delete item[key];
      }
    }));

  console.log(list);
};

const colToRemove = new Set(["N45", "E38"]);

const list = [{
  "THEDATE": "12/11/2022",
  "E38": 247,
  "E40": 212,
  "N45": 139,
  "N48": 10
}, {
  "THEDATE": "12/10/2022",
  "E38": 47,
  "E40": 22,
  "N45": 19,
  "N48": 66
}, {
  "THEDATE": "12/12/2022",
  "E38": 24,
  "E40": 21,
  "N45": 39,
  "N48": 34
}];

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }

Here is an IE compatible script:

function main() {
  var altered = list.map(function(item) {
    return Object.keys(item).reduce(function(acc, key) {
      if (colToRemove.indexOf(key) === -1) {
        acc[key] = item[key];
      }
      return acc;
    }, {})
  });

  console.log(altered);
};

var colToRemove = ["N45", "E38"];

var list = [{
  "THEDATE": "12/11/2022",
  "E38": 247,
  "E40": 212,
  "N45": 139,
  "N48": 10
}, {
  "THEDATE": "12/10/2022",
  "E38": 47,
  "E40": 22,
  "N45": 19,
  "N48": 66
}, {
  "THEDATE": "12/12/2022",
  "E38": 24,
  "E40": 21,
  "N45": 39,
  "N48": 34
}];

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }

  • Related