Json 到 Javascript 对象数组

我正在尝试创建一个 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" } ] } ] } ];

任何建议,将不胜感激


紫衣仙女
浏览 130回答 2
2回答

冉冉说

我建议使用递归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":[{"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"}]}]}]你可以把它传递给js。

温温酱

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))
打开App,查看更多内容
随时随地看视频慕课网APP