1

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));
``
1
  • please write a better question in order to understand what's happening here Commented Apr 16, 2022 at 14:22

2 Answers 2

2

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
    )
})
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

thanks a lot but one condition not working, 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: "FI", valueid:"2468" } . arrobj value and obj valueid , codevalue and cid same should return array of objects, condition2 in my problem state
which one, please specify it better
I have added in this link -> jsfiddle.net/miyavv/tz7nyf2b/4
@code123 try it now

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.