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

273
Views
How can i remove Objects if array elements has a object type?

I have array of objects data, in each object i have nested array, in that array i need to remove the object if element has a array.

Input Data :-

const data = [{"userDetails":[{"username":"Sai ","profileUrl":"","_id":4},{"username":"Mohamed Abu","profileUrl":"","_id":1},4,1]},{"userDetails":[{"username":"Sai ","profileUrl":"","_id":4},{"username":"Mohamed Abu","profileUrl":"","_id":1},4,1]},{"userDetails":[{"username":"Sai ","profileUrl":"","_id":4},4]},{"userDetails":[{"username":"Mohamed Abu","profileUrl":"","_id":1},1]}]

Expected Output Data : -

data = [
  {
    userDetails: [4, 1],
  },
  {
    userDetails: [4, 1],
  },
  { 
    userDetails: [4] 
  },
  { 
    userDetails: [1] 
  },
];

Please help me in these issue.

Thanks in advance.

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

The following snippet should do the job.

data.map(x => removeNestedObj(x))


function removeNestedObj(x)
{
    x.userDetails = x.userDetails.filter(y => Number.isInteger(y))
  return x;
}
about 4 years ago · Santiago Gelvez Report

0

const data = [{"userDetails":[{"username":"Sai ","profileUrl":"","_id":4},{"username":"Mohamed Abu","profileUrl":"","_id":1},4,1]},{"userDetails":[{"username":"Sai ","profileUrl":"","_id":4},{"username":"Mohamed Abu","profileUrl":"","_id":1},4,1]},{"userDetails":[{"username":"Sai ","profileUrl":"","_id":4},4]},{"userDetails":[{"username":"Mohamed Abu","profileUrl":"","_id":1},1]}];

let finalData = data.map(a => a.userDetails)  //get userDetails array
    .map(a => a.filter(x => typeof x !== 'object')) //remove objects from array
    .map(a => {
        return {userDetails: a} //recreate object with key userDetails
    });
    
console.log(finalData);

about 4 years ago · Santiago Gelvez Report

0

You can use map and filter and compare with typeof same as :

const data = [{
  "userDetails": [{
    "username": "Sai ",
    "profileUrl": "",
    "_id": 4
  }, {
    "username": "Mohamed Abu",
    "profileUrl": "",
    "_id": 1
  }, 4, 1]
}, {
  "userDetails": [{
    "username": "Sai ",
    "profileUrl": "",
    "_id": 4
  }, {
    "username": "Mohamed Abu",
    "profileUrl": "",
    "_id": 1
  }, 4, 1]
}, {
  "userDetails": [{
    "username": "Sai ",
    "profileUrl": "",
    "_id": 4
  }, 4]
}, {
  "userDetails": [{
    "username": "Mohamed Abu",
    "profileUrl": "",
    "_id": 1
  }, 1]
}]

// for removing object in userDetails
const result = data.map(obj => ({userDetails: obj.userDetails.filter(e => typeof e !== 'object')}))
console.log(result)

// for getting numbers in userDetails 
const result1 = data.map(obj => ({userDetails: obj.userDetails.filter(e => typeof e === 'number')}))
console.log(result1)

about 4 years ago · Santiago Gelvez 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!