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

107
Views
How to keep only one object in a nested object inside an array

I'm having an array look like this

 [
  { 
    object1:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1}
  },
  { 
    object2:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1}
  },
  { 
    object3:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1}
  },

 ]

Now i want to get rid of every nested item that have more than one item and keep only the first, like :

 [
  { 
    object1:{ childObj1:[grandChild1]}
  },
  { 
    object2:{ childObj2:[grandChild1]}
  },
  { 
    object3:{ childObj3:[grandChild1]}
  },

 ]

Which is the most effective way to do this?

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

0

const arr = [{
    object1: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj3: 't',
      childObj2: 'r'
    }
  },
  {
    object2: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj3: 't',
      childObj2: 'r'
    }
  },
  {
    object3: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj3: 't',
      childObj2: 'r'
    }
  },

];

console.log(arr.map(el => {
  const child = el[Object.keys(el)[0]];
  const firstProperty = {
    [Object.keys(child)[0]]: child[Object.keys(child)[0]]
  };
  return {
    [Object.keys(el)[0]]: firstProperty
  }
}));

Hope this is what you wanted.

about 4 years ago · Juan Pablo Isaza Report

0

Try this:

const arr = [{
    object1: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj2: {},
      childObj3: {}
    }
  },
  {
    object2: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj2: {},
      childObj3: {}
    }
  },
  {
    object3: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj2: {},
      childObj3: {}
    }
  },
]

function f(obj, startLevel, currentLevel = 0) {
  let value
  if (Array.isArray(obj)) {
    value = []
  } else if (typeof obj === 'object') {
    value = {}
  } else {
    value = obj
  }

  if (typeof obj === 'object') {
    for (let i in obj) {
      if (currentLevel >= startLevel) {
        if (Array.isArray(obj[i])) {
          value[i] = obj[i].length ? [f(obj[i][0], startLevel, currentLevel + 1)] : [];
        } else {
          value[i] = f(obj[i], startLevel, currentLevel + 1)
        }
        break;
      } else {
        value[i] = f(obj[i], startLevel, currentLevel + 1)
      }
    }
  }

  return value;
}

console.log(f(arr, 1));

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!