-2

Need to filter the arr based on check values. I am unable to filter inside the getItems function.

let check =  ["DIS_1","DIS_2","DIS_6","DIS_10"]
let arr = [{otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "first", cv: "cal2"},{cc: "DIS_2", cv: "cal3"}]},
          {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "second", cv: "cal5"},{cc: "DIS_10", cv: "cal6"}]},
          {otherProperty: "",cr: [{cc: "third", cv: "cal7"},{cc: "fourth", cv: "cal8"},{cc: "DIS_1", cv: "cal9"}]}
          ]

let expectedOutput = [{otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "DIS_2", cv: "cal3"}]},
          {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "DIS_10", cv: "cal6"}]},
          {otherProperty: "",cr: [{cc: "DIS_1", cv: "cal9"}]}
          ]

function getItems(arr) {
  console.log('--arr', arr)
}

console.log('expect', getItems(arr))
NeNaD
  • 18,172
  • 8
  • 47
  • 89
chidananda
  • 131
  • 7
  • `getItems` doesn’t return anything. Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Oct 04 '21 at 02:28
  • yeah, need to add logic inside getItems to return filtered array similar to expectedOutput – chidananda Oct 04 '21 at 02:31

2 Answers2

-1

You can do it with one liner:

const getItems = (arr) => arr.map(({cr, ...other_properties})=> ({ ...other_properties, cr: cr.filter(item=>check.includes(item.cc))}));

Here is the working example:

let check =  ["DIS_1","DIS_2","DIS_6","DIS_10"]
let arr = [
  {otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "first", cv: "cal2"},{cc: "DIS_2", cv: "cal3"}]},
  {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "second", cv: "cal5"},{cc: "DIS_10", cv: "cal6"}]},
  {otherProperty: "",cr: [{cc: "third", cv: "cal7"},{cc: "fourth", cv: "cal8"},{cc: "DIS_1", cv: "cal9"}]}
]
          
const getItems = (arr) => arr.map(({cr, ...other_properties})=> ({ ...other_properties, cr: cr.filter(item=>check.includes(item.cc))}));

console.log(getItems(arr));
NeNaD
  • 18,172
  • 8
  • 47
  • 89
-1

// let check =  ["DIS_1","DIS_2","DIS_6","DIS_10"];
let type = { DIS_1: "first", DIS_2: "two", DIS_6: "six", DIS_10: "ten" };
let arr = [{otherProperty: "",cr: [{cc: "DIS_1", cv: "cal1"},{cc: "first", cv: "cal2"},{cc: "DIS_2", cv: "cal3"}]},
          {otherProperty: "",cr: [{cc: "DIS_6", cv: "cal4"},{cc: "second", cv: "cal5"},{cc: "DIS_10", cv: "cal6"}]},
          {otherProperty: "",cr: [{cc: "third", cv: "cal7"},{cc: "fourth", cv: "cal8"},{cc: "DIS_1", cv: "cal9"}]}
          ]

// using array
// const res = arr.map(item => ({...item, cr : item.cr.filter(o => check.includes(o.cc))}));

// using object
const res = arr.map(item => ({...item, cr : item.cr.filter(o => Object.keys(type).includes(o.cc))}));

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0 }
navnath
  • 3,548
  • 1
  • 11
  • 19