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.

2 Answers 2

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"]))

Sign up to request clarification or add additional context in comments.

Comments

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);

5 Comments

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.
is your code functional programming? It looks so different, yet brilliant and short. Do I have to learn FP to understand your code?
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.
already accepted the answer. More than happy with the answer. I wish I could give you 5 more votes :)
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. stackoverflow.com/questions/40445853/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.