0

I have two array of objects in which if property grp from arrobj1 is

same as SERVICE and ISACTIVE is true from arrobj2, then return array of object using

javascript

Tried

let result = arrobj1.filter(e=> 
 arrobj2.some(i=> i.ISACTIVE===true && e.grp === i.SERVICE);
);

var arrobj1=[
  {
    id:"SetupFS",
    grp:"fs",
    title: "xxx"
  },
  {
    id:"ExtendFS",
    grp:"fs",
    title: "yyy"
  },
  {
    id:"RebootServer",
    grp:"os",
    title: "yyy"
  },
]

var arrobj2=[
{id:1, ISACTIVE:true, TASK:'SetupFS', SERVICE: "fs" }, 
{id:2, ISACTIVE:false, TASK:'RebootServer', SERVICE:"os" }, 
{id:3, ISACTIVE:false, TASK:'ExtendFS', SERVICE: "fs" }, 
]



Expected Result

[
 {
    id:"SetupFS",
    grp:"fs",
    title: "xxx"
  }
 
]

2
  • Your expected output meets the requirements you outlined at the start of the question, but I suspect you also want to check if e.id === i.TASK Commented Nov 26, 2022 at 10:29
  • Please update your question and explain why you feel your code is not right? If you get unexpected output, then what exactly is not according to the rules you have set out at the start? Commented Nov 26, 2022 at 11:25

2 Answers 2

1

You don't need filter for second item, you need to check if the item with corresponding index in arrobj1 with grp value equal to SERVICE value in arrobj2

var arrobj1=[
  {
    id:"SetupFS",
    grp:"fs",
    title: "xxx"
  },
  {
    id:"ExtendFS",
    grp:"fs",
    title: "yyy"
  },
  {
    id:"RebootServer",
    grp:"os",
    title: "yyy"
  },
]

var arrobj2=[
{id:1, ISACTIVE:true, TASK:'SetupFS', SERVICE: "fs" }, 
{id:2, ISACTIVE:false, TASK:'RebootServer', SERVICE:"os" }, 
{id:3, ISACTIVE:false, TASK:'ExtendFS', SERVICE: "fs" }, 
]

let result = arrobj2.filter((item, i) => 
 item.SERVICE === arrobj1[i].grp
);

console.log(result)

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

4 Comments

thanks but should INACTIVE is true ,
@code44, so you need to filter in arrobj2 not arrobj1, I just update the answser.
,, not like that, i should filter from arrobj1 only, if arrobj1.grp === arrobj2.service and arrobj.isactive === true then return that arrobj
I used this as arrobj2 , var arrobj2=[ {id:1, ISACTIVE:true, TASK:'SetupFS', SERVICE: "fs" }, {id:2, ISACTIVE:false, TASK:'RebootServer', SERVICE:"os" }, {id:3, ISACTIVE:true, TASK:'ExtendFS', SERVICE: "fs" }, ] used this code let result = arrobj1.filter((item, i) => item.grp === arrobj2[i].SERVICE && arrobj2[i].ISACTIVE === true );
0

A simpler method

// gets two results wit the equals
let filteredList = [];
for (const item of arrobj1) {
 // include TASK === item.id to get the expected answer
 const inArray = arrobj2.find(e => e.ISACTIVE && e.TASK === item.id &&  e.SERVICE === item.grp);
  if (inArray) {
    filteredList.push(item)
  }
}

console.log(filteredList)

with filters in the question it returns two items e => e.ISACTIVE && e.SERVICE === item.grp

0: Object
   id: "SetupFS"
   grp: "fs"
   title: "xxx"
1: Object
   id: "ExtendFS"
   grp: "fs"
   title: "yyy"

Hope it helps

but if this is not what was expected, I'll delete the answer

4 Comments

Hi Azzy thanks for reply, if pass the arrobj2 as var arrobj2=[ {id:1, ISACTIVE:true, TASK:'SetupFS', SERVICE: "fs" }, {id:2, ISACTIVE:false, TASK:'RebootServer', SERVICE:"os" }, {id:3, ISACTIVE:false, TASK:'ExtendFS', SERVICE: "fs" }, ] `, not working
is it not working with the second condition const inArray = arrobj2.find(e => e.ISACTIVE && e.TASK === item.id && e.SERVICE === item.grp); I had tried it on code sandbox, it was giving the right result, I will update the answer accordingly
may i know why i need to check ` // include TASK === item.id to get the expected answer` instead of two conditions
With e.ISACTIVE && e.SERVICE === item.grp it gets two results as given in the answer, the id of the items in first array matches with the TASK items in the second array, when viewed visually, you can use the filter condition which you feel serves the purpose

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.