0

I have an array of objects. i want to filter array based the object has the condition.

my array is as follow :

var data = [
{
  "name": "nitin",
  "r_id": "1",
  "t_id": "4"
},
{
 "name": "test",
  "r_id": "2",
  "t_id": "3"
},
{
  "name": "test1",
  "r_id": "2",
  "t_id": "4"
},
{
  "name": "test3",
  "r_id": "3",
  "t_id": "3"
},
{
  "name": "test2",
  "r_id": "1",
  "t_id": "1"
}]

and my object is as follows :

var obj = {
role:['1','2'],
type:['1','3']
}

where r_id is the role id and t_id is the type id

so i want the results whose role id is in 1 or 2 AND type id is in 1 or 3.

so mathematically role_id && type_id ((1||2)&&(1||3))

my output should like:

var result = [
{
  'name':'test',
  'r_id':2,
  't_id':3,
},
{
  'name':'test2',
  'r_id':1,
  't_id':1,
}];
3
  • 1
    do you have tried something? Commented Jan 13, 2017 at 13:18
  • So how do you check if a value is in an array? How do you use filter? Answer those and sounds like you will be closer to the answer. Commented Jan 13, 2017 at 13:20
  • ya i have tried first take object then do foreach of each array then inside of it i have did foreach of my user array but not getting any output.. so confuse about to get result Commented Jan 13, 2017 at 13:20

4 Answers 4

1

var data = [
{
  "name": "nitin",
  "r_id": "1",
  "t_id": "4"
},
{
 "name": "test",
  "r_id": "2",
  "t_id": "3"
},
{
  "name": "test1",
  "r_id": "2",
  "t_id": "4"
},
{
  "name": "test3",
  "r_id": "3",
  "t_id": "3"
},
{
  "name": "test2",
  "r_id": "1",
  "t_id": "1"
}]

var obj = {
role:['1','2'],
type:['1','3']
}

let result = data.filter(item=>{
	return obj.role.indexOf(item.r_id) > -1 && obj.type.indexOf(item.t_id) > -1
})

console.log(result)

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

Comments

0

var data = [
{
  "name": "nitin",
  "r_id": "1",
  "t_id": "4"
},
{
 "name": "test",
  "r_id": "2",
  "t_id": "3"
},
{
  "name": "test1",
  "r_id": "2",
  "t_id": "4"
},
{
  "name": "test3",
  "r_id": "3",
  "t_id": "3"
},
{
  "name": "test2",
  "r_id": "1",
  "t_id": "1"
}]

var obj = {
role:['1','2'],
type:['1','3']
}

var filterItems = data.filter(function(o){ 
   return obj.role.indexOf(o.r_id) != -1  && obj.type.indexOf(o.t_id) != -1; });

console.log(filterItems);

Comments

0

You could change obj a bit, for corresponding property names in the data, you have, then use Array#filter and check with Array#every for wanted item.

var data = [{ name: "nitin", r_id: "1", t_id: "4" }, { name: "test", r_id: "2", t_id: "3" }, { name: "test1", r_id: "2", t_id: "4" }, { name: "test3", r_id: "3", t_id: "3" }, { name: "test2", r_id: "1", t_id: "1" }],
    obj = { r_id: ['1', '2'], t_id: ['1', '3'] },
    keys = Object.keys(obj),
    result = data.filter(function (o) {
        return keys.every(function (k) {
            return obj[k].indexOf(o[k]) !== -1;
        });
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

You just need to iterate your data and see if the keys (r_id and t_id) are present in the obj (in the respective arrays). For this you can use Array.filter

var data = [{"name":"nitin","r_id":"1","t_id":"4"},{"name":"test","r_id":"2","t_id":"3"},{"name":"test1","r_id":"2","t_id":"4"},{"name":"test3","r_id":"3","t_id":"3"},{"name":"test2","r_id":"1","t_id":"1"}]

var obj = {
  role:['1','2'],
  type:['1','3']
}

var result = data.filter(function(element){
    return obj.role.indexOf(element.r_id) !== -1 && // element has valid role
           obj.type.indexOf(element.t_id) !== -1 // element has valid type
})

console.log(result)

Comments

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.