使用JavaScript对大数组进行分页。

huangapple go评论72阅读模式
英文:

Paginate big array with javascript

问题

I have a js array that will cause some issue to my electron app due to it's size, it will contain about 53000 objects. To avoid performances issues, how I can split it when data are loaded on application start? at the moment I'm getting the data from ad odbc access file and pushing it to an array on the front-end using vuejs.

background.js

ipcMain.on('init', (event, ...args) => {
    let suppliersData = false
    let clientsData = false
    suppliersdb.query('SELECT * FROM FANFOR0F_1')
        .then((results) => {
            console.log(results.length)
            //
            suppliersData = true
            //result.NEMA = result.NEMA.replace('§', '@')
            event.sender.send('suppliersData', results)            
        }).catch(e => { 
            console.log(e) 
            event.sender.send('databaseUpdateError', e)
        })
    clientsdb.query('SELECT * FROM PDECON0F_1')
        .then((results) => {
            console.log(results.length)
            clientsData = true
            event.sender.send('clientsData', results)
        }).catch(e => {
            console.log(e)
            event.sender.send('databaseUpdateError', e)
        })
    //
    if (suppliersData && clientsData) {
        event.sender.send('databaseUpdateCompleted')
    }
})

vue frontend-loading time mounted() hook

		window.ipcRenderer.send('init')
		window.ipcRenderer.receive('suppliersData', (data) => {
			this.suppliersDataAvailable = true
			this.store.suppliers = data
		})
		window.ipcRenderer.receive('clientsData', (data) => {
			console.log(data)
			this.clientsDataAvailable = true
			this.store.clients = data
		})

My idea is to split it into different pages or lazyload the content when page is scrolled, I'm implementing rxdb to have a local copy of the data and decrease loading time on application startup. Any suggestion will be appreciated.

英文:

I have a js array that will cause some issue to my electron app due to it's size, it will contain about 53000 objects. To avoid performances issues, how I can split it when data are loaded on application start? at the moment I'm getting the data from ad odbc access file and pushing it to an array on the front-end using vuejs.

background.js

ipcMain.on('init', (event, ...args) => {
    let suppliersData = false
    let clientsData = false
    suppliersdb.query('SELECT * FROM FANFOR0F_1')
        .then( (results) => {
            console.log(results.length)
            //
            suppliersData = true
            //result.NEMA = result.NEMA.replace('§', '@')
            event.sender.send('suppliersData', results)            
        }).catch( e => { 
            console.log(e) 
            event.sender.send('databaseUpdateError', e)
        })
    clientsdb.query('SELECT * FROM PDECON0F_1')
        .then( (results) => {
            console.log(results.length)
            clientsData = true
            event.sender.send('clientsData', results)
        }).catch( e => {
            console.log(e)
            event.sender.send('databaseUpdateError', e)
        })
    //
    if( suppliersData && clientsData ){
        event.sender.send('databaseUpdateCompleted')
    }
})

vue frontend-loading time mounted() hook

		window.ipcRenderer.send('init')
		window.ipcRenderer.receive('suppliersData', (data) => {
			this.suppliersDataAvailable = true
			this.store.suppliers = data
		})
		window.ipcRenderer.receive('clientsData', (data) => {
			console.log(data)
			this.clientsDataAvailable = true
			this.store.clients = data
		})

My idea is to split it into different pages or lazyload the content when page is scrolled, I'm implementing rxdb to have a local copy of the data and decrease loading time on application startup. Any suggestion will be appreciated.

答案1

得分: 2

我找到了一个更简单的解决方案,涉及到 .slice() 方法:

const array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14];
const arrays = [];
const arraysLengths = 4;
for (let i = 0; i < array.length; i=i+arraysLengths) {
    arrays.push(array.slice(i,i+arraysLengths));
};
console.log(arrays);  

一切都一样运行。除了你不再需要那个临时数组了。

英文:

i found an easier solution that involves .slice() method:

const array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14];
const arrays = [];
const arraysLengths = 4;
for (let i = 0; i &lt; array.length; i=i+arraysLengths) {
    arrays.push(array.slice(i,i+arraysLengths));
    
};
console.log(arrays);  

everything works the same. Except you don't need that temporary array anymore.

答案2

得分: 1

我有一个解决方案:

const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
//要拆分的主数组
const arrays = [];
//小数组
const arraysLengths = 4;
//每个小数组的元素数量
let array1 = []
//临时数组

for (let i = 0; i < array.length; i = i + arraysLengths) {
    for (let j = 0; j < arraysLengths; j++) {
        if ((i + j) <= array.length - 1) {
            array1.push(array[i + j])
        }
    };
    //这是拆分数组的代码

    arrays.push(array1);
    array1 = []
    //将数组传入另一个数组
};
console.log(arrays) //显示结果

您可以在arraysLengths中插入任何自然数,它都可以工作。您可以在arrays数组中找到所有的小数组。

英文:

I have a solution:

const array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14];
//main array that you want to split
const arrays = [];
//small arrays
const arraysLengths = 4;
//elements in one array
let array1 = [] 
//temporary array


for (let i = 0; i &lt; array.length; i=i+arraysLengths) {
    for (let j = 0; j &lt; arraysLengths; j++) {
        if ((i+j) &lt;= array.length - 1) {
            array1.push(array[i+j])
        }

    };
    //this is the code that splits arrays

arrays.push(array1);
array1=[]
//transfer arrays into another array
};
console.log(arrays)  //show result

You can insert any natural number in arrayLengths and it will work.
You can find all the arrays in arrays array

huangapple
  • 本文由 发表于 2023年5月22日 16:05:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定