Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
How to filter array of object and filter out values based on another array? Filtering should happen based on keys not values

I have an array of object which must be filtered based on another array, the keys are listed in the allowed array, pls help tired using object.entries and reduce but didn't work

const filter = _.filter;
const data = [{
    id: 1,
    row: [{
        id: 'a',
        name: 'ab',
        code: 'sdf',
        version: 1
      },
      {
        id: 'b',
        name: 'bc',
        code: 'def',
        version: 3
      },
      {
        id: 'c',
        name: 'cd',
        code: 'afd',
        version: 2
      },
    ]
  },
  {
    id: 2,
    row: [{
        id: 'd',
        name: 'ef',
        code: 'sdf',
        version: 1
      },
      {
        id: 'e',
        name: 'gh',
        code: 'def',
        version: 3
      },
      {
        id: 'f',
        name: 'ij',
        code: 'afd',
        version: 2
      },
    ]
  },
  {
    id: 3,
    row: [{
        id: 'g',
        name: 'kl',
        code: 'asd',
        version: 2
      },
      {
        id: 'h',
        name: 'mn',
        code: 'faf',
        version: 3
      },
      {
        id: 'i',
        name: 'op',
        code: 'dfs',
        version: 1
      },
    ]
  }
]

const allowed = ['id', 'name']

let result = [{
    id: 1,
    row: [{
        id: 'a',
        name: 'ab'
      },
      {
        id: 'b',
        name: 'bc'
      },
      {
        id: 'c',
        name: 'cd'
      },
    ]
  },
  {
    id: 2,
    row: [{
        id: 'd',
        name: 'ef'
      },
      {
        id: 'e',
        name: 'gh'
      },
      {
        id: 'f',
        name: 'ij'
      },
    ]
  },
  {
    id: 3,
    row: [{
        id: 'g',
        name: 'kl'
      },
      {
        id: 'h',
        name: 'mn'
      },
      {
        id: 'i',
        name: 'op'
      },
    ]
  }
]

result = data.filter(el => el.row.filter(elm => Object.fromEntries(allowed.map(k => [k, elm[k]]))));

console.log(result);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can create a new array with Array.map.

Logic

  • Map through the array.
  • Just spread operator to seperate out row key and rest of keys.
  • return an object with rest of keys and row key as with the Object.fromEntries

const data = [{
  id: 1,
  row: [
    { id: 'a', name: 'ab', code: 'sdf', version: 1 },
    { id: 'b', name: 'bc', code: 'def', version: 3 },
    { id: 'c', name: 'cd', code: 'afd', version: 2 },
  ]
},
{
  id: 2,
  row: [
    { id: 'd', name: 'ef', code: 'sdf', version: 1 },
    { id: 'e', name: 'gh', code: 'def', version: 3 },
    { id: 'f', name: 'ij', code: 'afd', version: 2 },
  ]
},
{
  id: 3,
  row: [
    { id: 'g', name: 'kl', code: 'asd', version: 2 },
    { name: 'mn', code: 'faf', version: 3 },
    { id: 'i', name: 'op', code: 'dfs', version: 1 },
  ]
}
]

const allowed = ['id', 'name'];

const result = data.map(({ row, ...rest }) => {
  return {
    ...rest,
    row: row.map(elm => Object.fromEntries(allowed.map(k => [k, elm[k]])))
  }
});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Long way but it works:

const data = [
    {id: 1, row: [
            {id: 'a', name: 'ab', code: 'sdf', version: 1},
            {id: 'b', name: 'bc', code: 'def', version: 3},
            {id: 'c', name: 'cd', code: 'afd', version: 2},
        ]
    },
    {id: 2, row: [
            {id: 'd', name: 'ef', code: 'sdf', version: 1},
            {id: 'e', name: 'gh', code: 'def', version: 3},
            {id: 'f', name: 'ij', code: 'afd', version: 2},
        ]
    },
    {id: 3, row: [
            {id: 'g', name: 'kl', code: 'asd', version: 2},
            {id: 'h', name: 'mn', code: 'faf', version: 3},
            {id: 'i', name: 'op', code: 'dfs', version: 1},
        ]
    }
];

const allowed = ['id', 'name'];

let res = [];
data.forEach((el) => {
    let obj = {};
    obj.id = el.id;
    obj["row"] = [];
    let row = buildArray(el.row);
    obj["row"].push(row);
    res.push(obj);
})

function buildArray(row) {  
  r = {};
  allowed.forEach((k) => {    
    r[k] = row[0][k];
  })
  return r;
}
console.log(res)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!