Home > front end >  how to get new array object based on condition like key exists in javascript
how to get new array object based on condition like key exists in javascript

Time:09-21

I would like to know how to create a new array object if the key exists in JavaScript.

I have two array objects, arr1 and arr2. In arr2, if the c value is true and the key of arr1 and a value are equal, push it to a new array object. If arr2 of key-value is true and matches with the key of arr1, then push to a new array in JavaScript.

var arr1 = {
    data:[
      {id:1, place: "IN", year: "2020", mode: "ON"},
      {id:3, place: "TH", year: "2022", mode: "OFF"},
      {id:5, place: "AU", year: "2025", mode: "ON"} 
    ]
};
var arr2=[
  {a: "place", c: true},
  {a: "year", c: true},
  {a: "mode", c: false},
]

var finals = [];

var result = arr1.data.map(e=>{
  arr2.forEach(i=>{
    if(i.c == true && Object.keys(e)){
     finals.push(e);
    }
  })
  return finals
})

Expected Output:

[
  {place: "IN", year: "2020"}
  {place: "TH", year: "2022"},
  {place: "AU", year: "2025"}
]

CodePudding user response:

You were almost there.

  1. arr1.data.map creates a new array,
  2. for each item in arr1, create a new object newObj,
  3. loop over each key in arr2
  4. add key.a with corresponding value in item, if key.c is true
  5. return newObj

var arr1 = {
    data:[
      {id:1, place: "IN", year: "2020", mode: "ON"},
      {id:3, place: "TH", year: "2022", mode: "OFF"},
      {id:5, place: "AU", year: "2025", mode: "ON"} 
    ]
};

var arr2=[
  {a: "place", c: true},
  {a: "year",  c: true},
  {a: "mode",  c: false},
];

var result = arr1.data.map(item => {  // 1
  let newObj = {};       // 2

  arr2.forEach(key => {  // 3
    if (key.c) {         // 4
      newObj[key.a] = item[key.a]
    }
  });

  return newObj          // 5
})

console.log(result)

CodePudding user response:

Logic

  • Get your required keys from arr2 with condition node.c === true.
  • map through arr1.data from each object in this array, create a new object with keys that we got from above logic.
  • This aggregated list will give you the required result.

var arr1 = {
  data: [
    { id: 1, place: "IN", year: "2020", mode: "ON" },
    { id: 3, place: "TH", year: "2022", mode: "OFF" },
    { id: 5, place: "AU", year: "2025", mode: "ON" }
  ]
}
var arr2 = [
  { a: "place", c: true },
  { a: "year", c: true },
  { a: "mode", c: false },
];
const keys = arr2.filter((node) => node.c).map((node) => node.a);
var result = arr1.data.map(e => {
  const obj =  keys.reduce((acc, curr) => {
    acc[curr] = e[curr];
    return acc;
  }, {});
  return obj;
})

console.log(result);

CodePudding user response:

Can be done using .map() and .reduce():

var arr1 = {
    data: [
      {id: 1, place: "IN", year: "2020", mode: "ON"},
      {id: 3, place: "TH", year: "2022", mode: "OFF" },
      {id: 5, place: "AU", year: "2025", mode: "ON" },
    ]
};

var arr2 = [
    {a: "place", c: true},
    {a: "year", c: true},
    {a: "mode", c: false},
];

const result = arr1.data.map(item =>
    Object.keys(item).reduce((acc, key) => {
      const isEnabled = arr2.some(({a, c}) => a === key && c === true);
      return isEnabled ? { ...acc, [key]: item[key]} : acc;
    }, {})
);

console.log(result);

CodePudding user response:

A reduce and forEach approach:

let arr1 = { data: [{ id: 1, place: "IN", year: "2020", mode: "ON" }, { id: 3, place: "TH", year: "2022", mode: "OFF" }, { id: 5, place: "AU", year: "2025", mode: "ON" }] };
let arr2 = [{ a: "place", c: true }, { a: "year", c: true }, { a: "mode", c: false }, ]

let finals = arr1.data.reduce((acc, itm) => {
  let obj = {};
  arr2.forEach(({a, c}) => {
    if (c) obj[a] = itm[a];
  })
  acc.push(obj)
  return acc
}, [])

console.log(finals)

CodePudding user response:

Couldn't resist adding another version to the mix, using Object.fromEntries

const arr1 = {    data:[      {id:1, place: "IN", year: "2020", mode: "ON"},      {id:3, place: "TH", year: "2022", mode: "OFF"},      {id:5, place: "AU", year: "2025", mode: "ON"}     ]};
const arr2=[  {a: "place", c: true},  {a: "year", c: true},  {a: "mode", c: false},]



const props = arr2.flatMap(p=> p.c ? p.a : []);
const finals = arr1.data.map(o=> Object.fromEntries(props.map(p=>[p,o[p]])));

console.log(finals);

  • Related