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

125
Views
JavaScript Map Object and Filter Object for nested objects

I wrote these two functions that maps and filter flat objects properties:

var mapObject = (obj, f, ctx) => {
  ctx = ctx || this;
  var result = {};
  Object.keys(obj).forEach(function(k) {
    result[k] = f.call(ctx, obj[k], k, obj);
  });
  return result;
};
var filterObject = (obj, f, ctx) => {
  ctx = ctx || this;
  Object.keys(obj).filter(v => {
    return (!f.call(ctx, obj[v], v, obj))
  }).forEach(v => {
    delete obj[v];
  })
  return obj;
}

These two functions can map "flat" properties (i.e. non nested objects) of and object or can filter non nested object properties in this way:

var mapObject = (obj, f, ctx) => {
  ctx = ctx || this;
  var result = {};
  Object.keys(obj).forEach(function(k) {
    result[k] = f.call(ctx, obj[k], k, obj);
  });
  return result;
};
var filterObject = (obj, f, ctx) => {
  ctx = ctx || this;
  Object.keys(obj).filter(v => {
    return (!f.call(ctx, obj[v], v, obj))
  }).forEach(v => {
    delete obj[v];
  })
  return obj;
}

console.log(mapObject({
  name: "james",
  surname: "joyce"
}, (v, k) => v.toUpperCase()))

console.log(filterObject({
  name: "james",
  surname: "joyce"
}, (v, k) => k != "name"))

console.log(mapObject(filterObject({
  name: "james",
  surname: "joyce"
}, (v, k) => k != "name"), (v, k) => v.toUpperCase()))

How to efficiently apply this to nested objects i.e. objects having objects properties?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think a more efficient way to apply this to nested objects is recursion, as an example:

const mapObject = (obj, f, ctx) => {
    ctx = ctx || this;
    var result = {};
    Object.keys(obj).forEach(function(k) {
        if (typeof obj[k] === 'object' && obj[k] !== null) {
            result[k] = mapObject(obj[k], f, ctx)
        } else {
            result[k] = f.call(ctx, obj[k], k, obj);
        }
    });
    return result;
};

console.log(mapObject({
    name: "james",
    surname: "joyce",
    obj: {
        objName: "hi"
    }
}, (v, k) => v.toUpperCase()))

const filterObject = (obj, f, ctx) => {
    ctx = ctx || this;
    Object.keys(obj).filter(v => {
        if (typeof obj[v] === 'object' && obj[v] !== null) {
            filterObject(obj[v], f, ctx)
        } else {
            return (!f.call(ctx, obj[v], v, obj))
        }
    }).forEach(v => {
        delete obj[v];
    })
    return obj;
}

console.log(filterObject({
    name: "james",
    surname: "joyce",
    obj : {
        name: "objName",
        surname: "objSurname",
    }
}, (v, k) => k != "name"))

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!