首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想将下面的代码分解成预期的格式

我想将下面的代码分解成预期的格式
EN

Stack Overflow用户
提问于 2022-05-17 08:20:32
回答 3查看 40关注 0票数 -2
代码语言:javascript
复制
[
    {
        "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"
    }
];

这是后端的响应,我想要下面的代码格式

代码语言:javascript
复制
[
    {   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:'' }
        ]
    },
    
];

在所有用例注释的对象属性值中,有多个用例我需要“一个使用说明”作为对象。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-05-17 08:38:00

好吧我试过了。你说过类似的事吗?

代码语言:javascript
复制
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);

票数 0
EN

Stack Overflow用户

发布于 2022-05-17 08:43:43

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2022-05-17 12:23:46

代码语言:javascript
复制
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
           };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72270507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档