Home > Back-end >  Unable to Push multiple values array into keys array into a single Array using javascript
Unable to Push multiple values array into keys array into a single Array using javascript

Time:05-17

I have a Keys Array and Multiple Values Array . I want push values array into keys Array . Example :-

data[0] :['ktCalender', 'presenter', 'emailId', 'topic', 'status']             // Keys

[1] : ['2022-05-05', 'abc', '[email protected]', 'cricket overview', 'sheduled'] // Values [3]: ['2022-05-04', 'xyz', '[email protected]', 'ApS', 'organized']. // Values

I want Answer like : [ {ktCalender:2022-05-05,presenter:'abc',emailId:'[email protected]',topic:'cricket overview',status:'sheduled'}, ktCalender:'2022-05-04',presenter:'xyx',emailId:'[email protected]',topic:'APS',status:'organized'},

]

CodePudding user response:

let data = ['ktCalender', 'presenter', 'emailId', 'topic', 'status'];
let Values = [];
Values[0] = ['2022-05-04', 'xyz', '[email protected]', 'ApS', 'organized'];
Values[1] = ['2022-05-05', 'abc', '[email protected]', 'cricket overview', 'sheduled']


let result = [];
Values.forEach((val) => {
    const obj = data.reduce((accumulator, element, index) => {
        return {...accumulator, [element]: val[index]};
    }, {});
    result.push(obj);
});
console.log(result);

  • Related