Home > Net >  how to retrive array of objects based on object using javascript
how to retrive array of objects based on object using javascript

Time:04-16

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

CodePudding user response:

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

CodePudding user response:

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

  • Related