1

I want to filter JSON to check if pre-defined keys are exist in that or not. php example code for that is :

$search = json_decode($request->get('search'),true);

            // Get only required params
            $search = array_filter($search, function($k){
                return $k == 'id' || $k == 'name' || $k == 'filterText' || $k == 'isEnable';
            }, ARRAY_FILTER_USE_KEY);

The abve code give me output when filterText and isEnable is present in query, output will be somthing like this (with key and value) :

{
    "isEnable": 1,
    "filterText": "a"
}

How do I accomplish this in javascript. I need code for javascript and not jquery

My json will look like :

{ question: 'aaa', answer: 'bbb' }

4 Answers4

0

You can use Array.prototype.filter(). The example below

function isBigEnough(value) {
  return value >= 10;
}

var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]

Reference

0

You have a prototype method on an array which can do this.

var asdf = ['asdf', 'bsdf', 'csdf']; asdf.filter(a => a == 'asdf');

Output:

['asdf']

Prakash
  • 471
  • 1
  • 8
  • 19
0

I am supposing you have an Array(or Array of objects) like this:

var search = [{ id: 123, name:'Some name', filterText:'my text', isInable:true }];

var res = search.filter(function(obj){
    return(obj.id || obj.name || obj.filterText || obj.isInable);
});

Now you will get a filtered Array in res.

EDIT

If you have only an object then you can use a simple trick:

var myObject = { id: 123, name:'Some name', filterText:'my text', isInable:true };

var search = [myObject]; //object to array (temporary)

var res = search.filter(function(obj){
    return(obj.id || obj.name || obj.filterText || obj.isInable);
});

if(res && res.length){
    res = res[0]; //array to object
}

So now in res you will have filtered object.

I hope this will help. Thanks.

Arpit Kumar
  • 2,179
  • 5
  • 28
  • 53
  • I have only object something like { question: 'aaa', answer: 'bbb' } and not an array – Nirav Majethiya Jun 21 '17 at 05:17
  • If my response have something like [ { question: 'aaa', answer: 'bbb', foo: 'bar', dummy: 'text' } ] and now if I use above method to filter only question and answer, i.e. something like { question: 'aaa', answer: 'bbb'}, it isn't working. It outputs whole string and filter isn't working – Nirav Majethiya Jun 21 '17 at 05:34
  • @NiravMajethiya You are confusing me. Please give the input and the output you want then only I will be able to answer you. – Arpit Kumar Jun 21 '17 at 05:38
  • input is something like { question: 'aaa', answer: 'bbb', foo: 'bar', dummy: 'text' }, where {foo: 'bar', dummy: 'text'} are wrong inputs and I want to filter that out. So when I use filter, I will match the input string and check if it has key 'question' then store that key and value both else remove key and value – Nirav Majethiya Jun 21 '17 at 05:42
0

You can write a simple function that will copy only the properties you care about from the input object to a new output object:

var validProperties = ['question', 'answer'];

function applyMask (mask, obj) {
    var output = {};
    for (var prop of mask) {
        if (!(obj[prop] == null)) {
            output[prop] = obj[prop];
        }
    }
    return output;
}


var test1 = {
    question: 'aaa',
    answer: 'bbb'
};

console.log(applyMask(validProperties, test1)); // Object { question: "aaa", answer: "bbb" }


var test2 = {
    question: 'aaa',
    wrongprop: 'bbb'
};

console.log(applyMask(validProperties, test2)); // Object { question: "aaa" }


var test3 = {
    wrongprop1: 'aaa',
    wrongprop2: 'bbb'
};

console.log(applyMask(validProperties, test3)); // Object {  }