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

139
Views
Compare json arrays and result in a simple array

I have two JSONS and i have to create an array from it

Array1: [{ label: "HEADER" value:"header" }, { label: "FOOTER" value:"footer" }]

Array2: [ { header: "Header 1", footer: "Footer 1" }, { header: "Header 2", footer: "Footer 2" }]

Expected Array(The label of Array1 will be the search criteria from Array2 Keys : [["Header 1","Header 2"],["Footer 1","Footer 2"]]

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

0

You could use Array.map() on Array1, then for each value in this array, run .map() again, returning all matching items in Array2.

We'd use Array.find() and String.includes() to do this.

const Array1 = [ { label: "HEADER", value:"header" }, { label: "FOOTER", value:"footer" } ]  
const Array2 = [ { header: "Header 1", footer: "Footer 1" }, { header: "Header 2", footer: "Footer 2" } ]

const result = Array1.map(({ label, value}) => { 
    return Array2.map((obj) => { 
        let key = Object.keys(obj).find(key => key.includes(value));
        return key ? obj[key]: null;
    })
})
console.log('Result:', result);
        
.as-console-wrapper { max-height: 100% !important; top: 0; }

Another example, this time Array2 has an entry with no footer property (as requested):

const Array1 = [ { label: "HEADER", value:"header" }, { label: "FOOTER", value:"footer" } ]  
const Array2 =  [ { header: "Header 1", footer: "Footer 1" }, { header: "Header 2" }]

const result = Array1.map(({ label, value}) => { 
    return Array2.map((obj) => { 
        let key = Object.keys(obj).find(key => key.includes(value));
        return key ? obj[key]: null;
    })
})
console.log('Result:', result);
        
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

I'm taking for granted that array1 values can only contain 'header' | 'footer' and that each array2 item has a header and a footer property since it's not specified. So it would be:

const array1 = JSON.parse(/* JSON1 file content */);
const array2 = JSON.parse(/* JSON2 file content */);

const result = array1.map(item1 => {
    const criteria = item1.label.toLowerCase();
    return array2.map(item2 => item2[criteria]);
});
about 4 years ago · Juan Pablo Isaza Report

0

let Array1=[{label:"HEADER",value:"header",},{label:"FOOTER",value:"footer",}] ,Array2=[{header:"Header 1",footer:"Footer 1"},{header:"Header 2",footer:"Footer 2"}]

let result = Array1.map(e => e.label.toLowerCase())
                   .map(s => Array2.map(n => n[s]))

console.log(result)

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!