'How to fetch the array of objects with array object and array list based on conditions using javascript

I have array of objects, and arrays

how to compare based on conditions using javascript

  1. fetch the array of objects having same value property

  2. then, based on value list If check cid is same, return both array of objects

  3. or then, based on value list Else check cid is different but includes of IN or FI return empty

    or return that array object

var listcode =["IN","FI"];
var listarr1 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SP", value: "B3456"},
  {id:4, name: "ben", cid: "FI", value: "C5678"},
]

var listarr2 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"},
]

Expected Output 
//same value , and has "IN" 
//listarr1
[]

//listarr2
//same value no include of `FI or IN` so return
[
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"}
] 

I tried

const checkIdMembers = list => {
    const idlist = ['IN', 'FI'];
    const resultarray = list
      .map((obj, i) => list.find((element, index) => {
        if (i !== index && element.value === obj.value && 
           (element.cid === obj.cid || idlist.includes(element.cid)) {
          return obj;
        }
      }))
      .filter(x => x);
    return resultarray;
  };
  
  
  const result = checkIdMembers(listarr1);
 



Solution 1:[1]

It think it's easier if you invert the order of the operations

const listcode =["IN","FI"];
const listarr1 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SP", value: "B3456"},
  {id:4, name: "ben", cid: "FI", value: "C5678"},
]

const listarr2 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"},
]

const listarr3 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "IN", value: "C5678"},
  {id:4, name: "ben", cid: "IN", value: "C5678"},
]



const checkIdMembers = data => {

const ALL = 'ALL'
 const dataObj  = data.reduce((res, d) => {

   const existingValue = res[d.value] || {}
   const existing = existingValue[d.cid] || []
   const existingAll = existingValue[ALL] || []
   
   existingValue[d.cid] = [...existing, d]
   
   if(!listcode.includes(d.cid)){
     existingValue[ALL] = [...existingAll, d]
   }
   res[d.value] = existingValue 
   return res
 }, {})
 return Object.values(dataObj).flatMap(v => {
    return Object.values(v).filter(group => group.length > 1)
 })
 
}

console.log(checkIdMembers(listarr1))
console.log(checkIdMembers(listarr2))
console.log(checkIdMembers(listarr3))

I realize that my answer wasn't correct so i edited it.

I was missing one of your prerequisite and I had to change the approach a bit

I transformed your initial array in an object like this

{
  "<value>" : {
     "<cid>" : [...elements with same cid and value]
      ....
     "ALL"   : [...elements with same value different cid but not in listcode]
  }
}

Once you have this object you can just take the values and than use flatMap on it to merge all groups of array that have length > 1

I hope this helps

Solution 2:[2]

I don't know if I understood correctly what you need but I think you can use a filter to get an array of the needed elements. It looks like this, conditions may vary according to your needs:

const data = [{
    id: 1,
    name: "dino",
    cid: "IN",
    value: "A1234"
  },
  {
    id: 2,
    name: "hem",
    cid: "IN",
    value: "B3456"
  },
  {
    id: 3,
    name: "zen",
    cid: "SG",
    value: "C5678"
  },
  {
    id: 4,
    name: "ben",
    cid: "SP",
    value: "C5678"
  },
]

const elementsWithIN = data.filter(el => el.cid === 'IN');
const elementsWithoutINOrSP = data.filter(el => !['IN', 'SP'].includes(el.cid))

console.log('elementsWithIN: ', elementsWithIN)
console.log('elementsWithoutINOrSP: ', elementsWithoutINOrSP)

Solution 3:[3]

So if I understood correctly this is what you want to achieve.

var listcode =["IN","FI"];
var listarr1 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SP", value: "B3456"},
  {id:4, name: "ben", cid: "FI", value: "C5678"},
]

var listarr2 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"},
]

//same value , and has "IN" 
//listarr1
const filterForSameIncluding = (codeToInclude, array) =>{
array = array.filter(o => codeToInclude.includes(o.cid));
var countList = array.reduce(function(p, c){
  p[c.value] = (p[c.value] || 0) + 1;
  return p;
}, {});
 return array.filter(function(obj){
  return countList[obj.value] > 1;
});
}

const filterForSameExcluding = (codeToExclude, array) =>{
array = array.filter(o => !codeToExclude.includes(o.cid));
var countList = array.reduce(function(p, c){
  p[c.value] = (p[c.value] || 0) + 1;
  return p;
}, {});
 return array.filter(function(obj){
  return countList[obj.value] > 1;
});
}
//listarr2
//same value no include of `FI or IN` so return

  
  
  const result1 = filterForSameIncluding(["IN"], listarr1);
  const result2 = filterForSameExcluding(["IN", "FI"], listarr2);

  console.log("same value with including codes=",result1)
  console.log("same value with excluding codes=", result2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 TechnoTech