微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

uniqBy在服务器端起作用,但在客户端不起作用

如何解决uniqBy在服务器端起作用,但在客户端不起作用

我的数组中充满了重复的键值,我想过滤以获取不同的值。

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",}
]

但是,当我在客户端调用时,HTML中的控制台显示“ uniqBy不是函数

我该如何解决?无论如何,我可以过滤我巨大的json filr>

解决方法

因为在客户端,您导入Loadash的方式有些不同,因此只有服务器端才能导入,以便在html头或正文中的客户端上使用loadash <script src="lodash.js"></script>以在浏览器中使用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。