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
Merge array of arrays containing value

I have an array of objects, each containing arrays and I want to merge them if each array contains display: true. I'm very new to array manipulation, and a nudge in the correct way would be very much appreciated.

Example array:

{
    "first": [
        {
            "name": "John",
            "surname": "Doe",
            "display": true
        },
        {
            "name": "Jane",
            "surname": "Doe",
            "display": false
        }
    ],
    "second": [
        {
            "name": "Sarah",
            "surname": "Doe",
            "display": true
        }
    ]
}

Example of expected result:

[
    {
        "name": "John",
        "surname": "Doe",
        "display": true
    },
    {
        "name": "Sarah",
        "surname": "Doe",
        "display": true
    }
]

I already have javascript code to set each display key to true/false based on search query. I just need the array merged to match the expected result.

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

0

You could get all values and filter the flat array.

const
    data = { first: [{ name: "John", surname: "Doe", display: true }, { name: "Jane", surname: "Doe", display: false }], second: [{ name: "Sarah", surname: "Doe", display: true }] },
    result = Object
        .values(data)
        .flat()
        .filter(({ display }) => display);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Use a reducer and filter the values within on the display property value. Finally flatten the result:

const mergedByDisplay = Object.entries({
    "first": [{
        "name": "John",
        "surname": "Doe",
        "display": true
      },
      {
        "name": "Jane",
        "surname": "Doe",
        "display": false
      }
    ],
    "second": [{
      "name": "Sarah",
      "surname": "Doe",
      "display": true
    }]
  })
  .reduce((acc, [_, val]) => [...acc, val.filter(v => v.display)], [])
  .flat();
console.log(mergedByDisplay);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!