我的数组中充满了重复的键值,我想过滤以获取不同的值。
JSON.file
20888行:
[
{
"table_schema": "pg",
"table_name": "type",
},
{
"table_schema": "pg",
"table_name": "type1",
},
{
"table_schema": "pg12",
"table_name": "pg_type",
}
]
Node.js函数:
//since I'm using to call to the client side, i cant use const uniqby = require("lodash.uniqby") as it will return me require is not defined in the html
const uniqBy = typeof window !== 'undefined'
? window.uniqBy
:require('lodash.uniqby');
let arrayUniq = [];
export const filteredArray = () => {
var uniqueALR = uniqBy(data, 'table_schema');
console.log(uniqueALR);
return uniqueALR ;
}
这在服务器端起作用,因为它返回了我:
[
{
"table_schema": "pg",
"table_name": "type",
},
{
"table_schema": "pg12",
"table_name": "pg_type",
}
]
但是,当我在客户端调用时,HTML中的控制台显示“ uniqBy不是函数”
我该如何解决?无论如何,我可以过滤我巨大的json filr>
答案 0 :(得分:0)
因为在客户端,您导入Loadash的方式有些不同,因此只有服务器端才能导入,以便在html头或正文中的客户端上使用loadash
<script src="lodash.js"></script>
以在浏览器中使用