Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

node.js - JavaScript filter array of objects based on property values

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...