简体   繁体   中英

i want to destructure the below code to expected formate

[
    {
        "id": "use1",
        "keyTag": "Use case notes",
        "label": "Role of Cellular IoT in Smart Lighting",
        "image": "/assets/images/resources/use-case-notes/Smart-Lighting.png",
        "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Smart-Lighting_1644835447168.png",
        "heroImage": "/assets/images/resources/Cavli-advantage-series.png",
        "link": "",
        "description": "Smart Lighting is an IoT technology that enables the automated response of lamps. Find out the current challenges in the market and the solutions by downloading this use case note.",
        "tags": [
            "Street lights",
            "energy saving",
            "Cavli Smart modules"
        ],
        "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Smart+Lighting_1645430650036.pdf"
    },
    {
        "id": "use2",
        "keyTag": "Use case notes",
        "label": "Role of Cellular IoT in Making Buildings Better",
        "image": "/assets/images/resources/use-case-notes/Smart-building.png",
        "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Smart-Building_1644835324542.png",
        "heroImage": "/assets/images/resources/Cavli-advantage-series.png",
        "link": "",
        "description": "Cavli Wireless has prepared an expert use case note on Smart Building Solutions. Get access to it to learn about the most updated advancements. The document is inclusive of solutions to business and industrial challenges faced by IoT solution makers for Smart Building Technology.",
        "tags": [
            "Smart connected",
            "building operations"
        ],
        "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Smart%20Building_1645430607718.pdf"
    },

 
    {
        "id": "ind1",
        "keyTag": "industry primer",
        "label": "The Future of eSIM for IoT Solutions",
        "image": "/assets/images/resources/industry-primer/eSIM-Technology.png",
        "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/E-sim_1644834792874.png",
        "heroImage": "/assets/images/resources/Cavli-advantage-series.png",
        "link": "",
        "description": "eSIM's ability to seamlessly connect devices and enhance the operational efficiencies of cellular networks has led to a fast-paced adoption of the technology.",
        "tags": [
            "eSIM",
            "iSIM",
            "Hubble eSIM"
        ],
        "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Sunset+of+2G+and+its+impact+on+global+IoT+Deployments_1645430817067.pdf"
    },
    {
        "id": "ind2",
        "keyTag": "industry primer",
        "label": "Sunset of 2G and its impact on global IoT Deployments",
        "image": "/assets/images/resources/industry-primer/Effect-of-2G-shutdown.png",
        "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/2G-Shut-Down_1644834052111.png",
        "heroImage": "/assets/images/resources/Cavli-advantage-series.png",
        "link": "",
        "description": "A brief overview on what is next for IoT/M2M connectivity when 2G shutdown becomes complete.",
        "tags": [
            "2G Shutdown",
            "Cellular LPWAN Applications"
        ],
        "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Sunset+of+2G+and+its+impact+on+global+IoT+Deployments_1645430817067.pdf"
    },

    {
        "id": "how1",
        "keyTag": "How to guides",
        "label": "How to Decide Between Sigfox and Cellular IoT?",
        "image": "/assets/images/resources/how-to-guides/Sigfox-and-Cellular-IoT.png",
        "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Sigfox-and-Cellular-IoT_1644833751743.png",
        "heroImage": "/assets/images/resources/Cavli-advantage-series.png",
        "link": "/resources-download.html?page=how-to-guides&label=How+to+Decide+Between+Sigfox+and+Cellular+IoT%3F",
        "description": "A guide on choosing between Sigfox & Cellular IoT for your IoT solution.",
        "tags": [
            "Cellular IoT",
            "Sigfox"
        ],
        "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/How%20to%20Decide%20Between%20Sigfox%20and%20Cellular%20IoT_1645431072070.pdf"
    },
    {
        "id": "how2",
        "keyTag": "How to guides",
        "label": "A Comparative Study between Cellular IoT and LoRa",
        "image": "/assets/images/resources/how-to-guides/cellular-iot-vs-lora.png",
        "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Cellular-IoT-and-LoRa_1644833032013.png",
        "heroImage": "/assets/images/resources/Cavli-advantage-series.png",
        "link": "/resources-download.html?page=how-to-guides&label=A+Comparative+Study+between+Cellular+IoT+and+LoRa",
        "description": "It’s a comparative study of the ongoing debate between Cellular IoT and LoRaWAN. While Cellular IoT has plenty of advantages over LoRa, this guide will help you determine which of the two connectivity options is best suited for your particular application",
        "tags": [
            "LoRaWAN",
            "NB-IoT",
            "LTE-M1"
        ],
        "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Cellular+IoT+vs+LoRa_1645425243080.pdf"
    },

    {
        "id": "ebo1",
        "keyTag": "Ebook",
        "label": "Re-imagining IoT like never before with Hubble99",
        "image": "/assets/images/resources/e-books/Re-imagining-IoT-like-never-before-with-Hubble99.png",
        "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/hubble99-ebook%20_1645179290728.png",
        "heroImage": "/assets/images/resources/Cavli-advantage-series.png",
        "link": "",
        "description": "To unlock the real potential of IoT and to get a billion devices connected, the currently fragmented ecosystem needs to be re-engineered, consolidated & thus re-imagined from start to end.",
        "tags": [
            "IoT adoption plan",
            "End-to-end solution"
        ],
        "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Hubble99+ebook+_1645427072024.pdf"
    }
];

This is the response from backend i want the below code formate

[
    {   id:use1,
        label:'use case notes',
        values:[
            { image:"",heroimage:'',tags:[],link:'',smImage:'' },
            { image:"",heroimage:'',tags:[],link:'',smImage:'' }
        ]
    },
    {   id:ebo1,
        label:'Ebook',
        values:[
            { image:"",heroimage:'',tags:[],link:'',smImage:'' }
        ]
    },

    {   id:indu1,
        label:'industry primer',
        values:[
            { image:"",heroimage:'',tags:[],link:'',smImage:'' },
            { image:"",heroimage:'',tags:[],link:'',smImage:'' }
        ]
    },

    {   id:how1,
        label:'how to guides',
        values:[
            { image:"",heroimage:'',tags:[],link:'',smImage:'' },
            { image:"",heroimage:'',tags:[],link:'',smImage:'' }
        ]
    },
    
];

there are multiple-use cases I need 'one use note' as an object inside that object property values of all use case notes

Ok I've tried something. Do you speak about something like that?

 const fromBackend = [ { "id": "use1", "keyTag": "Use case notes", "label": "Role of Cellular IoT in Smart Lighting", "image": "/assets/images/resources/use-case-notes/Smart-Lighting.png", "s3image": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Smart-Lighting_1644835447168.png", "heroImage": "/assets/images/resources/Cavli-advantage-series.png", "link": "", "description": "Smart Lighting is an IoT technology that enables the automated response of lamps. Find out the current challenges in the market and the solutions by downloading this use case note.", "tags": [ "Street lights", "energy saving", "Cavli Smart modules" ], "s3Url": "https://uploads-hubblethings.s3.eu-west-1.amazonaws.com/Smart+Lighting_1645430650036.pdf" }]; const retArr = []; for( let i = 0; i< fromBackend.length; i++){ const curr = fromBackend[i]; retArr.push( { id: curr.id, label: curr.keyTag, values:[ { image: curr.image, heroimage: curr.heroImage, tags: curr.tags, link: curr.link, smImage: curr.s3image } ] } ); }; console.log(retArr);

function format(response, excludeKeys = ['id','label']) {
    const formate = [];
    response.forEach(data => {
       let formateItem = formate.find(formateItem => formateItem.id === data.id);
       const value = Object.keys(data).filter(key => !excludeKeys.includes(key)).reduce((a, key) => { a[key] = data[key]; return a; }, {});
       if(formateItem) {
          formateItem.values.push(value);
       } else {
          formateItem = excludeKeys.reduce((a, key) => { a[key] = data[key]; return a; }, {});
          formateItem.values = [value];
          formate.push(formateItem)
       }
    });
    return formate;
}
const response = []; // your response
const formate = format(response); //get what you want
let result = newData.reduce(function (r, a) {
                r[a.keyTag] = r[a.keyTag] || [];
                r[a.keyTag].push(a);
                return r;
            }, Object.create(null));

            let keyTags = Object.keys(result);
            let fArray = [];
            keyTags.map((item) => {
                fArray.push({
                    label: item,
                    value:''
                });
            });
            for (let i = 0; i < newData.length; i++) {
                for (let j = 0; j < fArray.length; j++) {
                   if(newData[i].keyTag == fArray[j].label) {
                       fArray[j].id
                    fArray[j]['value'] =  newData[i].keyTag;
                   }
                }
            }
           return {
               data:newData,
               mappedData:fArray
           };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM