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

95
Views
How to filter an array which is inside an object which has other details as well

I have an object

const object = {
 name:'XYZ',
 Others: 'XYZ',
 numbers:[2,3,4,5,6,5]
}

I want to filter numbers array which is inside the object and return the object with filtered array like this in which array has numbers 5 only.

{
 name:'XYZ',
 Others: 'XYZ',
 numbers:[5,5]
}

If I use filter in JavaScript like this

object.numbers.filter()

It returns only numbers array not complete object with other details like name. What is the best way to implement it?

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

0

Just assign object.numbers with filter return like:

const object = {
 name:'XYZ',
 Others: 'XYZ',
 numbers:[2,3,4,5,6,5]
};
object.numbers = object.numbers.filter(el => el === 5);
console.log(object);

about 4 years ago · Juan Pablo Isaza Report

0

to make it work with this.setState you have to create a new Object:

let object = {
 name:'XYZ',
 Others: 'XYZ',
 numbers:[2,3,4,5,6,5]
}

let updatedObject = {...object,numbers:object.numbers.filter(e => e === 5)}
console.log(updatedObject)
// this.setState(updateObject)

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!