亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Json 到 Javascript 對象數組

Json 到 Javascript 對象數組

PHP
紫衣仙女 2023-07-21 18:16:02
我正在嘗試創建一個 javascript 對象作為引導樹視圖的輸入。我有 php 從 mysql 獲取數據,并將結果編碼為以下結構:{"Company 1":{"Production":["Brands","Categories","Products","Stocks"],"Sales":["Customers","Orders","Staffs","Stores"]},"Company 2":{"Production":["Brands","Categories","Products","Stocks"],"Sales":["Customers","Orders","Staffs","Stores"]}}生成該 json 的 PHP 代碼:$databases=[];foreach($result as $row){$database=$row["database"];$schema=$row["schema"];$table=$row["object"];if(!array_key_exists($database, $databases))    $databases[$database]=[];if(!array_key_exists($schema, $databases[$database]))    $databases[$database][$schema]=[];array_push($databases[$database][$schema], $table);}echo json_encode($databases);但我正在努力將該 json 結構放入所需的 JavaScript 對象的嵌套數組中。以下是所需的結構:[ { text: "Company 1", nodes: [ { text: "Production", nodes: [ { text: "Brands" }, { text: "Categories" }, { text: "Products" }, { text: "Stocks" } ] }, { text: "Sales", nodes: [ { text: "Customers" }, { text: "Orders" }, { text: "Staffs" }, { text: "Stores" } ] } ] }, { text: "Company 2", nodes: [ { text: "Production", nodes: [ { text: "Brands" }, { text: "Categories" }, { text: "Products" }, { text: "Stocks" } ] }, { text: "Sales", nodes: [ { text: "Customers" }, { text: "Orders" }, { text: "Staffs" }, { text: "Stores" } ] } ] } ];任何建議,將不勝感激
查看完整描述

2 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

我建議使用遞歸flatItem函數來做到這一點。我還介紹了一個array_map_with_keys輔助函數,因為 PHP 自己的array_map函數會忽略鍵,而在您的情況下,我們需要它。



function array_map_with_keys($callback, $array){

  return array_map($callback, array_keys($array), $array);

}


function flatItem($key, $value) {

  if(is_array($value)){

    return 

      ["text" => $key,

       "nodes" => array_map_with_keys("flatItem", $value)

      ];  

  }else{

    return ["text" => $value];

  }

}


$converted = array_map_with_keys("flatItem", $databases);


echo json_encode($converted);

結果就是你所期望的:


[{"text":"Company 1","nodes":[{"text":"Production","nodes":[{"te

xt":"Brands"},{"text":"Categories"},{"text":"Products"},{"text":

"Stocks"}]},{"text":"Sales","nodes":[{"text":"Customers"},{"text

":"Orders"},{"text":"Staffs"},{"text":"Stores"}]}]},{"text":"Com

pany 2","nodes":[{"text":"Production","nodes":[{"text":"Brands"}

,{"text":"Categories"},{"text":"Products"},{"text":"Stocks"}]},{

"text":"Sales","nodes":[{"text":"Customers"},{"text":"Orders"},{

"text":"Staffs"},{"text":"Stores"}]}]}]

你可以把它傳遞給js。


查看完整回答
反對 回復 2023-07-21
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

const data = {

  "Company 1": {

    "Production": ["Brands", "Categories", "Products", "Stocks"],

    "Sales": ["Customers", "Orders", "Staffs", "Stores"]

  },

  "Company 2": {

    "Production": ["Brands", "Categories", "Products", "Stocks"],

    "Sales": ["Customers", "Orders", "Staffs", "Stores"]

  }

}


function converter(data) {

  return Object.entries(data).reduce((converted, [key, val]) => {

    const element = {

      text: key,

      nodes: [...Object.entries(val).map(([key2, val2]) => {

        return {

          text: key2,

          nodes: [...Object.values(val2).map(val3 => {

            return {

              text: val3

            }

          })]

        }

      })]

    }

    converted.push(element);

    return converted

  }, []);

}


console.log(converter(data))


查看完整回答
反對 回復 2023-07-21
  • 2 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號