Home > database >  How to get array of objects only if array values matches with object array key
How to get array of objects only if array values matches with object array key

Time:10-07

I have array arr1, arr2, arr3 and object array objarr

I would like to get array of objects based on condition

i.e when passing arr1, object array objarr should return only matched keys of arr1

i.e when passing arr2, object array objarr should return only matched keys of arr2 (if wrk is passed parameter only show that is true )

i.e when passing arr3, object array objarr should return only matched keys of arr3

var arr1=["name", "place"]
var arr2=["details", "wrk"]
var arr3=["name"]

var objarr=[
  {id:1, name: "ayan", place: "MY", details: "finance" , wrk: true},
  {id:2, name: "mike", place: "AU", details: "teaching", wrk: false },
  {id:3, name: "john", place: "DE", details: "service" , wrk: true}
]

function outputData(arr1){
  var final=[];
  var result = objarr.map(e=>{
   if(arr1.includes(Object.keys(e)){
    final.push(e)
   }
  })

return  final
;}

Expected Output;
//for arr1 is passed
[
  {name: "ayan", place: "MY"},
  {name: "mike", place: "AU"},
  {name: "john", place: "DE"}
]

//for arr2 is passed
[
  {details: "finance", wrk:true},
  {details: "service", wrk: true}
]

//for arr3 is passed
[
  {name: "ayan"},
  {name: "mike"},
  {name: "john"}
]

CodePudding user response:

var arr1 = ["name", "place"];
var arr2 = ["details", "wrk"];
var arr3 = ["name"];

var objarr = [
  { id: 1, name: "ayan", place: "MY", details: "finance", wrk: true },
  { id: 2, name: "mike", place: "AU", details: "teaching", wrk: false },
  { id: 3, name: "john", place: "DE", details: "service", wrk: true }
]

function outputData(arr) {
  var final = [];
  objarr.forEach(obj => {
      const temp = {};
      arr.forEach(key => {
          if(obj[key]){
              temp[key]= obj[key];
          }
      });
      if(Object.keys(temp).length===arr.length){
      final.push(temp);}
  });

  return final;
}
console.log('arr1',outputData(arr1));
console.log('arr2',outputData(arr2));
console.log('arr3',outputData(arr3));

CodePudding user response:

You can easily achieve the result using reduce and for..of loop

var arr1 = ["name", "place"];
var arr2 = ["details", "wrk"];
var arr3 = ["name"];

var objarr = [
  { id: 1, name: "ayan", place: "MY", details: "finance", wrk: true },
  { id: 2, name: "mike", place: "AU", details: "teaching", wrk: false },
  { id: 3, name: "john", place: "DE", details: "service", wrk: true },
];

function getData(target, source) {
  return target.reduce((acc, curr) => {
    const obj = {};
    for (let prop of source) {
      obj[prop] = curr[prop];
    }
    if (Object.keys(obj).includes("wrk")) {
      if (obj["wrk"]) acc.push(obj);
    } else acc.push(obj);
    return acc;
  }, []);
}

console.log(getData(objarr, arr1));
console.log(getData(objarr, arr2));
console.log(getData(objarr, arr3));
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

Try this solution:

function outputData(arr) {
  return objarr.map(obj => Object.fromEntries(arr.map(k => [k, obj[k]])))
}

CodePudding user response:

Since you need to select only specific nodes from objarr, going for Array.map willnot give the result. Use Some other looping logic for that.

I used Array.reduce here.

Logic

  • Loop through objarr with Array.reduce.
  • Loop through the parementer array.
  • Check if the value for each node from the parameter array in the object in objarr array.
  • If the value is truthy, add that to an object.
  • If the length of keys of this object is same as the length of parameter array, push this object to accumulator.

var arr1 = ["name", "place"]
var arr2 = ["details", "wrk"]
var arr3 = ["name"]

var objarr = [
  { id: 1, name: "ayan", place: "MY", details: "finance", wrk: true },
  { id: 2, name: "mike", place: "AU", details: "teaching", wrk: false },
  { id: 3, name: "john", place: "DE", details: "service", wrk: true }
]

function outputData(arr) {
  var result = objarr.reduce((acc, currNode) => {
    const resp = {};
    arr.forEach((option) => {
      if (currNode[option]) {
        resp[option] = currNode[option]
      }
    })
    if(Object.keys(resp).length === arr.length) {
      acc.push(resp)
    }
    return acc
  }, []);
  return result;
}
console.log(outputData(arr1));
console.log(outputData(arr2));
console.log(outputData(arr3));

  • Related