简体   繁体   English

从JavaScript中的多维数组获取数组值

[英]Getting array values from multidimensional array in JavaScript

I'm in need of some minor assistance. 我需要一些小帮助。 I'm having trouble getting an array (larray3) populated with two other array objects (larray1 and larray2) to pass both from data.js into the subsequent model.js and view.js. 我在让一个数组(larray3)填充了另外两个数组对象(larray1和larray2)以将两者都从data.js传递到后续的model.js和view.js时遇到麻烦。 Data.js correctly builds the multidimensional array however when the results are received in model.js/view.js I only receive the results for larray1. Data.js可以正确构建多维数组,但是当在model.js / view.js中接收到结果时,我只会收到larray1的结果。 Because only the first values come thru I cannot tell if both larray1 and larray2 are actually passing thru. 因为只有第一个值通过,所以我无法确定larray1和larray2是否都通过。 Can someone please tell me how I should alter my syntax in either model.js or view.js to access both array values or what else I could change? 有人可以告诉我如何更改model.js或view.js中的语法以访问两个数组值,或者我还能更改什么? Thank you in advance. 先感谢您。

data.js. data.js。

function getCountries(done) {
var sqlite3 = require('sqlite3').verbose();
var file = 'db/locations.sqlite3';
var db = new sqlite3.Database(file);
var larray1 = [];
var larray2 = [];
var larray3 = [];

db.all('SELECT * FROM Country', function(err, rows) {
    //  This code only gets called when the database returns with a response.
    rows.forEach(function(row) {
        larray1.push(row.CountryName);
        larray2.push(row.CountryCode);
    })
larray3.push(larray1);
larray3.push(larray2);
return done(larray3[0], larray3[1]);
});
db.close();
}

model.js model.js

function Countries(done) {
//Pull location values from data
return getCountries(done);
}

view.js view.js

function viewCountries() {

var viewCou =  Countries(function(results) {
    // Code only gets triggered when Countries() calls return done(...); 
    var container = document.getElementById('country-select');
    var fragment = document.createDocumentFragment();

    results.forEach(function(loc, index) { 
        var opt = document.createElement('option');
        opt.innerHTML = loc;
        opt.value = loc;
        fragment.appendChild(opt);
    });
    container.appendChild(fragment);
})
}

In data.js you send two arguments to the done callback: 在data.js中,您将两个参数发送到done回调中:

return done(larray3[0], larray3[1]);

This done function is passed through in your model.js: 这个done函数在您的model.js中传递:

return getCountries(done);

And that done is passed in from view.js: done是从view.js传递过来的:

Countries(function(results) { // ...

So it is this anonymous function ( function(results) {...} ) that is called in data.js. 因此,就是在data.js中调用的匿名函数( function(results) {...} )。 But notice that this function only has one parameter, so you're doing nothing with the second argument that data.js sends. 但是请注意,此函数只有一个参数,因此您对data.js发送的第二个参数不执行任何操作。 result gets the value of larray3[0] , but larray3[1] is not captured anywhere. result获取larray3[0]的值,但larray3[1]未被捕获。

You could solve this in different ways. 您可以用不同的方法解决此问题。 Personally, I think the design with two arrays is wrong from the start. 我个人认为从一开始就有两个数组的设计是错误的。 I would not separate data that belongs in pairs (name and code) into two different arrays. 我不会将成对(名称和代码)的数据分成两个不同的数组。

Instead make an array of objects, and pass that single array around: 而是创建一个对象数组,并传递该单个数组:

In data.js: 在data.js中:

rows.forEach(function(row) {
    larray1.push({
        name: row.CountryName,
        code: row.CountryCode
    });
})
return done(larray1);

In view.js: 在view.js中:

    opt.textContent = loc.name;
    opt.value = loc.code;

Side-note: .textContent is preferred over .innerHTML when assigning plain text. 旁注:分配纯文本时, .textContent优于.innerHTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM