JavaScript filter array of objects based on property values

59
March 04, 2021, at 07:20 AM

I have an array of objects in javascript. The contents look like this;

obj_array = [{
        "DATA_ID": 1,
        "DATA_NAME": "Dim",
        "DATA_BB_TYP": 2,
        "DATA_MAC": "5474",
    },
    {
        "DATA_ID": 3,
        "DATA_NAME": "Spre",
        "DATA_BB_TYP": 33,
        "DATA_MAC": "8e30",
    },
    {
        "DATA_ID": 2,
        "DATA_NAME": "Dimb",
        "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 want to retain some objects and remove the rest. If the object property DATA_BB_TYP is 2 or 34, the objects will be retained. THe other objects are removed. The outcome of the obj_array will look like this;

obj_array_retained = 
[{
        "DATA_ID": 1,
        "DATA_NAME": "Dim",
        "DATA_BB_TYP": 2,
        "DATA_MAC": "5474",
    },
    {
        "DATA_ID": 2,
        "DATA_NAME": "Dimb",
        "DATA_BB_TYP": 2,
        "DATA_MAC": "45e8",
    },
    {
        "DATA_ID": 6,
        "DATA_NAME": "Sens",
        "DATA_BB_TYP": 34,
        "DATA_MAC": "d004",
    }
]

I am using node.js v6.91.

EDIT: Someone suggested to me using filter to solve this kind of problem. Answers using filter technique would be most welcomed.

Answer 1

You can try the below methods 1 and 2:

Method 1: (Using filter)

Note: This will return a new array and won't modify the original array.

var obj_array = [{ 
            "DATA_ID": 1, 
            "DATA_NAME": "Dim", 
            "DATA_BB_TYP": 2, 
            "DATA_MAC": "5474", 
        }, 
        { 
            "DATA_ID": 3, 
            "DATA_NAME": "Spre", 
            "DATA_BB_TYP": 33, 
            "DATA_MAC": "8e30", 
        }, 
        { 
            "DATA_ID": 2, 
            "DATA_NAME": "Dimb", 
            "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 retained = [2, 34]; 
 
    var new_obj_array = obj_array.filter(function(obj) { 
      if(retained.indexOf(obj.DATA_BB_TYP) === -1) { 
        return false; 
      } 
       
      return true; 
    }); 
 
    console.log(new_obj_array);

Method 2: (Basic version if you intend to use this in browser since filter won't be supported by old browsers)

var obj_array = [{ 
        "DATA_ID": 1, 
        "DATA_NAME": "Dim", 
        "DATA_BB_TYP": 2, 
        "DATA_MAC": "5474", 
    }, 
    { 
        "DATA_ID": 3, 
        "DATA_NAME": "Spre", 
        "DATA_BB_TYP": 33, 
        "DATA_MAC": "8e30", 
    }, 
    { 
        "DATA_ID": 2, 
        "DATA_NAME": "Dimb", 
        "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 retained = [2, 34]; 
 
for(var i = obj_array.length - 1; i >= 0; i--) { 
  var obj = obj_array[i]; 
  if(retained.indexOf(obj.DATA_BB_TYP) === -1) { 
    obj_array.splice(i, 1); 
  } 
} 
 
console.log(obj_array);

READ ALSO
How to kill a query running by pd.read_sql and connected by sqlalchemy (or mysql.connector)

How to kill a query running by pd.read_sql and connected by sqlalchemy (or mysql.connector)

I have a Python API that receives a request from PHP code and executes some queries with pdread_sql() in a MariaDB 10

66
How to run a file in VS Code?

How to run a file in VS Code?

I am learning PHP and I have decided to use VS CodeIs there any way I can run the files without a compiler? I have looked in the output section but nothing is there

60
Kotlin - OnSharedPreferenceChangeListener

Kotlin - OnSharedPreferenceChangeListener

I learn to develop my first Android appI choose Kotlin, just because

67
Is it possible to zip up and send a local (to the app) directory without requiring the user to select one?

Is it possible to zip up and send a local (to the app) directory without requiring the user to select one?

I'm attempting to create an app that can access a directory in the app's directory, zip it with JSZip and send it to an API at the press of a single button

69