第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Json 到 Javascript 對象數(shù)組

Json 到 Javascript 對象數(shù)組

PHP
紫衣仙女 2023-07-21 18:16:02
我正在嘗試創(chuàng)建一個 javascript 對象作為引導(dǎo)樹視圖的輸入。我有 php 從 mysql 獲取數(shù)據(jù),并將結(jié)果編碼為以下結(jié)構(gòu):{"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 結(jié)構(gòu)放入所需的 JavaScript 對象的嵌套數(shù)組中。以下是所需的結(jié)構(gòu):[ { 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貢獻(xiàn)1877條經(jīng)驗 獲得超1個贊

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



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);

結(jié)果就是你所期望的:


[{"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。


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

TA貢獻(xiàn)1752條經(jīng)驗 獲得超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))


查看完整回答
反對 回復(fù) 2023-07-21
  • 2 回答
  • 0 關(guān)注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號