得到"未定义"来自.each循环中的javascript数组

时间:2015-07-17 00:26:52

标签: javascript jquery arrays undefined

我已经看过一些类似的问题,但没有一个能够让我更接近解决方案。

我有这样的数据来自我的电子商务API: (请记住,我无法控制数据是如何发送给我的,因此我无法在"来源处更改任何内容。")

Object {
url: "http://pathtomyimage.jpg", name: ""}
name: ""url:  "http://pathtomyimage.jpg"

__proto__: Object__defineGetter__: __defineGetter__() { [native code] }__defineSetter__: __defineSetter__() { [native code] }__lookupGetter__: __lookupGetter__() { [native code] }__lookupSetter__: __lookupSetter__() { [native code] }constructor: Object() { [native code] }hasOwnProperty: hasOwnProperty() { [native code] }isPrototypeOf: isPrototypeOf() { [native code] }propertyIsEnumerable: propertyIsEnumerable() { [native code] }toLocaleString: toLocaleString() { [native code] }toString: toString() { [native code] }valueOf: valueOf() { [native code] }get __proto__: __proto__() { [native code] }set __proto__: __proto__() { [native code] }

这些本质上是我想在.each语句中循环并获取url的图像。我坦率地不知道Proto的所有东西是什么?但我对输出任何内容并不感兴趣。

这就是我正在做的事情:

$(data.images).each(function(index, value) {

console.log(this.url); // This is what is outputting the array you see above 

});

我收到了网址,但我也得到了#34;未定义"如果我尝试在.each循环中执行此操作,则会破坏图像...

$(data.images).each(function(index, value) {
$('#myContainer').append("<img src='" + this.url + "'>");
});

提前感谢您的建议和帮助。

编辑 - 我被要求发布整个字符串。如果我console.log(数据)这就是我得到的。它是一大堆...好吧,东西。我没有展开每个对象,但我相信这会让你了解整个对象的结构。在我尝试将它配对到我遇到麻烦的地方之前,但如果你相信整个字符串会有所帮助,那么就在这里。

Object
    availability: Objectcode: "M"__proto__: Object__defineGetter__: __defineGetter__() { [native code] }__defineSetter__: __defineSetter__() { [native code] }__lookupGetter__: __lookupGetter__() { [native code] }__lookupSetter__: __lookupSetter__() { [native code] }constructor: Object() { [native code] }hasOwnProperty: hasOwnProperty() { [native code] }isPrototypeOf: isPrototypeOf() { [native code] }propertyIsEnumerable: propertyIsEnumerable() { [native code] }toLocaleString: toLocaleString() { [native code] }toString: toString() { [native code] }valueOf: valueOf() { [native code] }get __proto__: __proto__() { [native code] }set __proto__: __proto__() { [native code] }

    averageDealerMargin: "0.50"
    currency: "USD"
    customFields: Array[0]
    image: "http://pathtoaimage.jpg"
    // Here's the trouble maker...
    images: Array[2]0: Objectname: ""url: "http://pathtoanotherimage.jpg"__proto__: Object1: Array[0]length: 2__proto__: Array[0]
    locale: "en-US"
    name: "ProductName"
    partNumber: "700"
    price: "19.99"
    priceDisplay: "$19.99"
    shippingMethod: "3"
    weight: Object
        units: "lb"
         value: "1.8"
__proto__: Object__proto__: Object

如果以前没有完全清楚道歉,我真的非常感谢这里的帮助。我一直在敲打这个,持续约2天。

还有一个编辑...... 这里的一个答案确实点击了我没有考虑过没有函数返回的东西。进一步扩展我如何使用这些数据是因为我首先检查我引用的产品是否合法。这是Shopatron API FWIW,文档一直缺乏,这将是一个很好的方式...所以无论如何,你会在脚本顶部看到对它的引用,如下所示:

var partNumber = '<?php echo $productID; ?>';
    $(document).ready(function() {
        Shopatron.getProduct({
            partNumber: partNumber
        },{
            success: function(p) {
                outputProductName(p);
                outputProductImage(p);
                outputProductAdditionalImages(p);
                outputProductPrice(p);
                outputDescription(p);
                outputSpecs(p);
            },
            templateFriendly : false                                
        }
    );

现在我的功能如下:

function outputProductAdditionalImages(data) {
    $('#myContainer').append("<img src='" + this.url + "'>");
});

2 个答案:

答案 0 :(得分:2)

尝试以这种方式使用$.each

$.each(data.images, function(i, obj) {
    if (obj.url) {
        $('#myContainer').append("<img src='" + obj.url + "'>");
    }
});

看看是否有诀窍。

答案 1 :(得分:1)

我认为您面临以下情况:

简而言之,function中的javascript没有return值,返回undefined。以图为例:

enter image description here

另外,您可以参考其他answer