我不明白这个错误:dataobj不是一个函数(I dont understand this error: dataobj is not a function)

编程入门 行业动态 更新时间:2024-10-28 17:13:44
我不明白这个错误:dataobj不是一个函数(I dont understand this error: dataobj is not a function)

js代码

var dataObj = {}; var shipDataObj = []; function shipData(shipDataObj){ for(i=0;i<shipDataObj.length;i++){ dataObj.push({ addressLine1 : shipDataObj[i].addressLine1, addressLine2 : shipDataObj[i].addressLine2, city : shipDataObj[i].city, state : shipDataObj[i].state, country : shipDataObj[i].country, zip : shipDataObj[i].zipCode, contactNum : shipDataObj[i].contactNumber }); } } appServices.getAddress(userData.customerId).then(function (data){ if (data){ console.log(data); $scope.shipDataObj = data; shipData(data); console.log(dataObj); } });

我只需要data.I中的7个属性。我在shipDataObj中复制了数据对象并调用了一个名为shipData的函数。 shipData将推送shipDataObj中的属性。 shipDataObj可能有两个索引,如0和1,每个索引都有7个属性。这7个属性我必须在dataObj中推送,这样我才能只显示这些属性。

我不明白为什么这个错误即将到来dataObj不是一个函数。

js code

var dataObj = {}; var shipDataObj = []; function shipData(shipDataObj){ for(i=0;i<shipDataObj.length;i++){ dataObj.push({ addressLine1 : shipDataObj[i].addressLine1, addressLine2 : shipDataObj[i].addressLine2, city : shipDataObj[i].city, state : shipDataObj[i].state, country : shipDataObj[i].country, zip : shipDataObj[i].zipCode, contactNum : shipDataObj[i].contactNumber }); } } appServices.getAddress(userData.customerId).then(function (data){ if (data){ console.log(data); $scope.shipDataObj = data; shipData(data); console.log(dataObj); } });

I need only 7 properties from data.I copied data object in shipDataObj and called a function named shipData. shipData will push the properties in shipDataObj. shipDataObj may have two indexes like 0 and 1 each of them have those 7 properties.these 7 properties I have to push in dataObj so that I can display only these properties.

I dont understand why this error is coming dataObj is not a function.

最满意答案

dataObj变量的值是对象引用,对象没有.push()方法。 如果你真的希望它是一个数组,它的初始化应该是这样的

var dataObj = [];

The value of your dataObj variable is an object reference, and objects don't have a .push() method. If you really want it to be an array, its initialization should look like

var dataObj = [];

更多推荐

本文发布于:2023-08-02 21:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   错误   一个函数   error   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!