Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
how to retrive array of objects based on object using javascript

How to get array of objects based on object and arraylist using javascript

return the array of objects based on two conditions

1.if arrobj value is equal to obj valueid and cid value should not include only listcode value

2.else if arrobj value is equal to obj valueid and codevalue is equal to cid

else return []

should follow above conditions and return arrayobject using javscript

var listcode =["IN","FI", "FR"];
var arrobj =[
  {id:1, name: "jan", cid: "IN", value: "1234"},
  {id:2, name: "feb", cid: "SG", value: "2468"},
  {id:3, name: "mar", cid: "SP", value: "2468"},
  {id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj={
  id:5, name: "zen", codevalue: "SP", valueid:"2468"
}

Expected Output
[
  {id:2, name: "feb", cid: "SG", value: "2468"},
  {id:3, name: "mar", cid: "SP", value: "2468"},
]

var listcode =["IN","FI","FR"];
var arrobj1 =[
  {id:1, name: "jan", cid: "IN", value: "1234"},
  {id:2, name: "feb", cid: "FI", value: "2468"},
  {id:3, name: "mar", cid: "IN", value: "2468"},
  {id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj1={
  id:5, name: "zen", codevalue: "SP", valueid:"2468"
}
Expected Output
[]

const result = arrobj.filter(e => e.value === obj.valueid
      && listcode.includes(e.cid));
``
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Is possible to achieve that by filtering the arrobj.

var listcode =["IN","FI", "FR"];
var arrobj =[
  {id:1, name: "jan", cid: "IN", value: "1234"},
  {id:2, name: "feb", cid: "SG", value: "2468"},
  {id:3, name: "mar", cid: "SP", value: "2468"},
  {id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj={
  id:5, name: "zen", codevalue: "SP", valueid:"2468"
}

const result = arrobj.filter((item) => {
  return item.value === obj.valueid &&
    (
      !listcode.includes(item.cid)
      || item.cid === obj.codevalue
    )
})
about 4 years ago · Juan Pablo Isaza Report

0

You did everything right, just add a negation before !includes

// data1
var listcode1 = ["IN","FI", "FR"];
var arrobj1 = [
  {id:1, name: "jan", cid: "IN", value: "1234"},
  {id:2, name: "feb", cid: "SG", value: "2468"},
  {id:3, name: "mar", cid: "SP", value: "2468"},
  {id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj1 = { id:5, name: "zen", codevalue: "SP", valueid:"2468"}


// data2
var listcode2 = ["IN","FI","FR"];
var arrobj2 = [
  {id:1, name: "jan", cid: "IN", value: "1234"},
  {id:2, name: "feb", cid: "FI", value: "2468"},
  {id:3, name: "mar", cid: "IN", value: "2468"},
  {id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj2 = { id:5, name: "zen", codevalue: "SP", valueid:"2468"}

function getNewArray(inputList, obj, listCodes) {
  const result = inputList.filter(x => x.value === obj.valueid && (!listCodes.includes(x.cid) || x.cid === obj.codevalue))
  return result;
}


console.log(getNewArray(arrobj1, obj1, listcode1))
console.log(getNewArray(arrobj2, obj2, listcode2))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!