How to merge two json return arrays?

// 
var field = [{
    field: "",
    display: "host",
    visible: true,
    type: "checkbox"
}, {
    field: "",
    display: "department",
    visible: true,
    type: "input"
}]



// list
var list = [{
    id: 1,
    host: "1.1 .1 .1",
    department: ""
}]

merge two arrays and return an array similar to the following

[{
        id: 1,
        host: {
            field: "",
            display: "host",
            value: "1.1 .1 .1",
            visible: true,
            type: "checkbox"
        },
        department: {
            field: "",
            display: "department",
            value: "",
            visible: true,
            type: "input"
        }
    }
]


const result = list.map((item) => {
    Object.keys(item).forEach((key) => {
        const filterArr = field.filter((fieldItem) => fieldItem.display === key)
        if (filterArr.length > 0) {
            filterArr[0].value = item[key];
            item[key] = filterArr[0];
        }        
    });
    return item;
});
console.log(result);

var field = [{
    field: '',
    display: 'host',
    visible: true,
    type: 'checkbox'
}, {
    field: '',
    display: 'department',
    visible: true,
    type: 'input'
}]

// list
var list = [{
    id: 1,
    host: '1.1 .1 .1',
    department: ''
}]

function mergeData(list,field){
    let result = [];
    list.forEach(obj=>{ //list
        
        let temp = {};
        field.forEach(item => { // listfield
            
            temp[item.display] = Object.assign({},item,{value:obj[item.display]})
        });
        
        result.push(Object.assign({id:obj.id},temp));
    });
    
    return result;
}

mergeData(list,field)

Brother you said merge json here can be done with arguments, none of the returned JSON data is regarded as a parameter and then create a new array combined with for in loop.


Hi, ideas for reference

const fieldMap = new Map(field.map(d => [d.display, d]))
const result = list.map(x =>
  Object.keys(x).reduce((o, k) => {
    o[k] = fieldMap.has(k) ? { ...fieldMap.get(k), value: x[k] } : x[k]
    return o
  }, {})
)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3705f-2c041.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3705f-2c041.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?