简体   繁体   English

javascript array.push内部循环-结果不是数组?

[英]javascript array.push inside for loop--result not array?

I'm trying to populate an array by using a for loop to access certain records in a database and then combine the contents of some DB fields with text to create each array element. 我试图通过使用for循环访问数据库中的某些记录来填充数组,然后将一些DB字段的内容与文本结合起来以创建每个数组元素。 Here's what I have: 这是我所拥有的:

var numbers = [7,8];
var phsw = [];
for (var i=0,len=numbers.length;i<len;i++) {
    selectObj = cObj.select().from("wp_posts").where('ID=?', numbers[i]);
    result1 = cObj.exec(selectObj);
    var resource = result1[0].guid;
    var title = result1[0].post_title;
    var tnTxt = result1[0].post_title;
    var tn = resource.replace(/\.jpg/,"-150x150.jpg");
    phsw.push({mytitle:'" + title + "', mythumbnail:'" + tn + "', mytntxt:'" + tnTxt + "', myresource:'" + resource +"'});
    }

This creates what I thought was an array, except it's apparently not really an array. 这创建了我认为是数组的东西,除了它显然不是一个数组。 When I use console.log(phsw) to see what's in it I get this: 当我使用console.log(phsw)看看里面有什么时,我得到了:

[{mytitle:'title', mythumbnail:'imagefile1tnlink', mytntxt:'thumbtxt1', myresource:'imagefile1link'},{mytitle:'title2', mythumbnail:'imagefile2tnlink', mytntxt:'thumbtxt2', myresource:'imagefile2link'}] [{mytitle:'title',mythumbnail:'imagefile1tnlink',mytntxt:'thumbtxt1',myresource:'imagefile1link'},{mytitle:'title2',mythumbnail:'imagefile2tnlink',mytntxt:'thumbtxt2' '}]

where I should get this: 我应该在哪里得到这个:

[object Object] [对象对象]

if it was really an array (right??). 如果它确实是一个数组(对吗?)。

I'm using this in Application Craft, and the phsw array is being created with server side javascript and then being passed to a callback on the application side, but when it gets back to the app side, I can't use it to populate a widget because it's not really an array. 我在Application Craft中使用它,并且phsw数组是使用服务器端javascript创建的,然后传递给应用程序端的回调,但是当它返回到应用程序端时,我无法使用它来填充小部件,因为它实际上不是数组。 How can I make sure phsw is an array? 如何确定phsw是数组?

And yes, I'm sure this code isn't the most clean or efficient way to do this; 是的,我确定这段代码不是执行此操作的最简洁或最有效的方法。 my (rather rusty) experience is in PHP and MySQL as opposed to javascript, so I'm always open to suggestions of better ways to do things! 我(相当生疏)的经验是在PHP和MySQL中而不是在javascript中,因此我总是乐于接受更好的做事方法的建议!

No everything is correct. 没有什么是正确的。 It is in fact an array containing exactly 2 objects. 实际上,它是一个仅包含2个对象的数组。

Your expectation that you should get [object Object] is wrong. 您对应该获得[object Object]的期望是错误的。

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

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