4

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"]]

4 Answers 4

3

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; }

Sign up to request clarification or add additional context in comments.

2 Comments

it seems to work but if Array2 has less data like this [ { header: "Header 1", footer: "Footer 1" }, { header: "Header 2" }] I wanted the result to be like[["Header 1","Header 2"],["Footer 1",null]] . Is that possible?
Sure, that's a small change. I've updated the answer and included a second snippet with that use case. Hope that help!
1

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]);
});

Comments

1

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)

Comments

1

A simple double Array.map will help.

Logic

  • First Array.map will return the values from first array arr1.
  • Second Array.map will return elements from second array arr2 with keys as value from arr1

const arr1 = [{ label: "HEADER", value: "header" }, { label: "FOOTER", value: "footer" }]
const arr2 = [{ header: "Header 1", footer: "Footer 1" }, { header: "Header 2", footer: "Footer 2" }];
const output = arr1.map(item => arr2.map(node => node[item.value]));
console.log(output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.