'Create excel-like data structure from JSON object Javascript Typescript

Hi I would like to convert a JSON type of data to CSV or Excel-like data with columns

here is JSON that contains the date name of the product and the price

let products = [
    [
        { date: '2022-05-09', name: 'laptop', price: '2245.430531634567' },
        { date: '2022-05-10', name: 'laptop', price: '2343.5108884928645' },
        { date: '2022-05-11', name: 'laptop', price: '2072.1086793072855' },
        { date: '2022-05-12', name: 'laptop', price: '1961.701582831061' },
        { date: '2022-05-13', name: 'laptop', price: '2014.4182339084884' },
        { date: '2022-05-14', name: 'laptop', price: '2056.273914657406' },
        { date: '2022-05-15', name: 'laptop', price: '2145.7066657035143' },
        { date: '2022-05-16', name: 'laptop', price: '2022.7259340437865' },
        { date: '2022-05-17', name: 'laptop', price: '2090.409245952252' },
        { date: '2022-05-18', name: 'laptop', price: '1993.262079076959' }
    ],
    [
        { date: '2022-05-09', name: 'usbCable', price: '0.9999087550995304' },
        { date: '2022-05-10', name: 'usbCable', price: '0.9997913625904791' },
        { date: '2022-05-11', name: 'usbCable', price: '0.9958721390751034' },
        { date: '2022-05-12', name: 'usbCable', price: '0.9976093690492182' },
        { date: '2022-05-13', name: 'usbCable', price: '0.9982388360597627' },
        { date: '2022-05-14', name: 'usbCable', price: '0.9987798583521733' },
        { date: '2022-05-15', name: 'usbCable', price: '0.9990687532601263' },
        { date: '2022-05-16', name: 'usbCable', price: '0.9987786971425011' },
        { date: '2022-05-17', name: 'usbCable', price: '0.998909095447568' },
        { date: '2022-05-18', name: 'usbCable', price: '0.998884287678029' }
    ],
    [
        { date: '2022-05-09', name: 'server', price: '30296.953400053055' },
        { date: '2022-05-10', name: 'server', price: '31022.905412225315' },
        { date: '2022-05-11', name: 'server', price: '28936.35503111219' },
        { date: '2022-05-12', name: 'server', price: '29047.751003853846' },
        { date: '2022-05-13', name: 'server', price: '29283.103811543002' },
        { date: '2022-05-14', name: 'server', price: '30101.266445984806' },
        { date: '2022-05-15', name: 'server', price: '31305.1125686764' },
        { date: '2022-05-16', name: 'server', price: '29862.918241291864' },
        { date: '2022-05-17', name: 'server', price: '30425.85727679598' },
        { date: '2022-05-18', name: 'server', price: '29421.647697692864' }
    ]
];

I would like to get this type of data after converting:

date;laptop;usbCable;server
2022-05-09;2245.430531634567;0.94545;30296.953400053055
2022-05-10;2247.430531634567;0.95545;31296.953400053055
2022-05-11;2243.430531634567;0.97545;32296.953400053055
2022-05-12;2243.430531634567;0.92545;34296.953400053055

I was browsing lodash library to have a conversion like this but didn't manage to find anything as well was checking json2csv converters but they always give me flat type of data output.

enter image description here



Solution 1:[1]

ok got it but maybe is not the most optimal:

function getItemByDate(jsObjects, date) {
    let result = jsObjects.find(obj => {
        return obj.date === date;
    });
    return result;

}

function convertToCsv(productsArray){
    let lineArray = [];

    // create header of csv
    lineArray.push('date;');
    productsArray.forEach(headerItem => {
        lineArray[0] += headerItem[0].symbol + ';';
    });


    // populate columns
    productsArray[0].forEach(item => {
        lineArray.push(item.date + ';');
        for (let i = 0; i < productsArray.length; i++) {
            let itemByDate = getItemByDate(productsArray[i], item.date);
            lineArray[lineArray.length - 1] += (itemByDate? itemByDate.price : '') + ';';
        }
    });
    return lineArray;
}

Solution 2:[2]

You shouldn't use a window.scrollTo(0, document.body.scrollHeight); because you don't want scroll an entire website (window) but just a certain div.

I'm able to scroll a job list in your sample site in both directions - up and down - with a below code:

document.getElementById('win0divHRS_AGNT_RSLT_I$grid$0').scrollTop = 7000;

The win0divHRS_AGNT_RSLT_I$grid$0 is a container with jobs which you want to scroll.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karol Be
Solution 2 1_bug