1

I would like to remove some properties from an array of javascript objects. Here is the array of objects.

obj_array = [{
        "DATA_ID": 1,
        "DATA_NAME": "Jim",
        "DATA_BB_TYP": 2,
        "DATA_MAC": "5474",
    },
    {
        "DATA_ID": 3,
        "DATA_NAME": "Fro",
        "DATA_BB_TYP": 33,
        "DATA_MAC": "8e30",
    },
    {
        "DATA_ID": 2,
        "DATA_NAME": "Jimb",
        "DATA_BB_TYP": 2,
        "DATA_MAC": "45e8",
    },
    {
        "DATA_ID": 4,
        "DATA_NAME": "Kht1",
        "DATA_BB_TYP": 35,
        "DATA_MAC": "58d0",
    },
    {
        "DATA_ID": 6,
        "DATA_NAME": "Sens",
        "DATA_BB_TYP": 34,
        "DATA_MAC": "d004",
    }
]

I have this string array which specifies what properties to remove.

var str_array_criteria = ["DATA_BB_TYP", "DATA_MAC"];

After removal, the array of object will look like this;

obj_array_removed = [{
        "DATA_ID": 1,
        "DATA_NAME": "Jim",        
    },
    {
        "DATA_ID": 3,
        "DATA_NAME": "Fro",
    },
    {
        "DATA_ID": 2,
        "DATA_NAME": "Jimb",
    },
    {
        "DATA_ID": 4,
        "DATA_NAME": "Kht1",
    },
    {
        "DATA_ID": 6,
        "DATA_NAME": "Sens",
    }
]

I am using node.js v6.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295

2 Answers2

1

Here you go,

var obj_array = [{
        "DATA_ID": 1,
        "DATA_NAME": "Jim",
        "DATA_BB_TYP": 2,
        "DATA_MAC": "5474",
    },
    {
        "DATA_ID": 3,
        "DATA_NAME": "Fro",
        "DATA_BB_TYP": 33,
        "DATA_MAC": "8e30",
    },
    {
        "DATA_ID": 2,
        "DATA_NAME": "Jimb",
        "DATA_BB_TYP": 2,
        "DATA_MAC": "45e8",
    },
    {
        "DATA_ID": 4,
        "DATA_NAME": "Kht1",
        "DATA_BB_TYP": 35,
        "DATA_MAC": "58d0",
    },
    {
        "DATA_ID": 6,
        "DATA_NAME": "Sens",
        "DATA_BB_TYP": 34,
        "DATA_MAC": "d004",
    }
];

    var str_array_criteria = ["DATA_BB_TYP", "DATA_MAC"];

    var new_obj_array = obj_array.map(function(obj) {
      str_array_criteria.forEach(function(prop) {
        delete obj[prop];
      });
      
      return obj;
    });

    console.log(new_obj_array);
Aruna
  • 11,959
  • 3
  • 28
  • 42
  • Amazing. The speed at which you can answer and the brevity of your code. I should learn about map, filter one of these days. Not easy to get used to them. – guagay_wk Nov 06 '16 at 01:49
  • is your code functional programming? It looks so different, yet brilliant and short. Do I have to learn FP to understand your code? – guagay_wk Nov 06 '16 at 03:29
  • 1
    Hi mate, it's not FP. They are plain JavaScript and inbuilt methods of Array. You can learn more about array methods to have fun with them. Once you are happy with this answer, please accept it or let me know if you have any issues. – Aruna Nov 06 '16 at 03:31
  • already accepted the answer. More than happy with the answer. I wish I could give you 5 more votes :) – guagay_wk Nov 06 '16 at 03:33
  • I am trying to do the reverse. Instead of having a criteria to remove properties, I would like to have a criteria to retain properties. I cannot figure out what to replace `delete obj[prop];` to achieve this. I asked another related question. In the mean time, I am trying to figure out an appropriate solution. http://stackoverflow.com/questions/40445853/retain-certain-properties-from-array-of-objects – guagay_wk Nov 06 '16 at 04:04
1

You can use underscore to achieve this. It's much simpler and more readable

obj_array.map(obj => _.omit(obj,["DATA_BB_TYP", "DATA_MAC"]))
rohit salaria
  • 196
  • 1
  • 13