Home > Mobile >  How to fetch the array of objects with array object and array list based on conditions using javascr
How to fetch the array of objects with array object and array list based on conditions using javascr

Time:04-14

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

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

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 checkIdMembers = data => {
 const filtered = data.filter(d => !listcode.includes(d.cid))
 
 return Object.entries(filtered.reduce((res, d) => {
   const existing = res[d.value] || []
   res[d.value] = [...existing, d]
   return res
 }, {})).filter(([value, arr]) => arr.length > 1).flatMap(([_, v]) => v)

}

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

  • Related