簡體   English   中英

將鍵值對轉換為所需對象的數組

[英]Transforming key value pair to array of desired objects

我有一個鍵值對為的JSON:

      let countByView= {
          "count1": "0",
          "count2": "230",
          "count3": "246",
          "count4": "172",
          "view1": "2017",
          "view2": "2018",
          "view3": "2019",
          "view4": "2020"
        }

我想變成

 let countByView=
        [
          {
            "count1": 0,
            "view1": 2017
          },
          {
            "count1": 230,
            "view1": 2018
          },
          {
            "count1": 246,
            "view1": 2019
          },
          {
            "count1": 172,
            "view1": 2020
          }
        ]

我嘗試使用下面的代碼,但由於無法正確轉換而無法獲得預期的結果。請讓我知道還有更好的解決方案。

  let result = Object.keys(countByView).map(e => {
        let ret = {};
        ret[e] = obj[e];
        return ret;
    });

console.log(result);

您想要的數組的長度將是countByView鍵的數量的一半。 使用長度,您可以使用for循環或Array#from生成有序數組:

 const countByView = {"count1":"0","count2":"230","count3":"246","count4":"172","view1":"2017","view2":"2018","view3":"2019","view4":"2020"} const length = Object.keys(countByView).length / 2 const result = Array.from({ length }, (_, i) => ({ count: countByView[`count${i + 1}`], view: countByView[`view${i + 1}`] })) console.log(result) 

您可以對具有數字的相同命名對象使用哈希表。

 var countByView = { count1: "0", count2: "230", count3: "246", count4: "172", view1: "2017", view2: "2018", view3: "2019", view4: "2020" }, hash = Object.create(null), result = Object.keys(countByView).reduce(function (r, k) { var [ , key, value] = k.match(/^(\\D+)(\\d+)$/); if (!hash[value]) { r.push(hash[value] = {}); } hash[value][key] = countByView[k]; return r; }, []); console.log(result); 

您可以簡單地遍歷該json對象的Object.keys並添加一些邏輯來獲取該結構。

 let countByView= { "count1": "0", "count2": "230", "count3": "246", "count4": "172", "view1": "2017", "view2": "2018", "view3": "2019", "view4": "2020" }; var keys = Object.keys(countByView); var resArray = []; for(var i=0; i<keys.length; i++){ var key = keys[i]; var obj = {}; if(key.indexOf('count') !== -1){ obj[key] = countByView[key]; obj['view'+(i+1)] = countByView['view'+(i+1)]; resArray.push(obj); } } console.log(resArray); 

您的示例具有以索引結尾的標簽。 您尚未指定它始終為1、2 ...還是可以為任何數字。 在下面的代碼中,我假定它可以是任何數字(只要countview具有相同的數字)。

 let countByView= { "count1": "0", "count2": "230", "count3": "246", "count4": "172", "view1": "2017", "view2": "2018", "view3": "2019", "view4": "2020" } function transform(input) { return Object .keys(input) .filter( key => key.indexOf('count') === 0) .reduce( (list, key) => { // Using a regexp to split the key into name and index. let item = /^([az]+)([0-9]+)$/.exec(key); if (!item) return console.warn('illegal key', key); let index = item[2]; // push the data to the list, also converts the value to a number. list.push({ count : input['count' + index ], view : +input['view' + index ] }) return list; },[] ); } console.log(transform(countByView)); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM