javascript - TypeError: push() is not a function - Stack Overflow

I'm trying to push an item to an array but it's not working. When I run the code, I get this

I'm trying to push an item to an array but it's not working. When I run the code, I get this error:

--Uncaught TypeError: data.allItems[type].push is not a function--

var data = {
    allItems: {
      exp: [],
      inc: []
    },
    totals: {
      exp: 0,
      inc: 0
    },
    budget: 0,
    percentage: -1
  };

  return {
      addItem: function(type, des, val) {
          var newItem, ID;

          // Create new ID
          if (data.allItems[type].length > 0) {
              ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
          } else {
              ID = 0;
          }

          // Create new item based on 'inc' or 'exp' type
          if (type === 'exp') {
              newItem = new Expense(ID, des, val);
          } else if (type === 'inc') {
              newItem = new Ine(ID, des, val);
          }

          // Push it into our data structure
          data.allItems[type].push(newItem);

          // Return the new element
          return newItem;
      },

I'm trying to push an item to an array but it's not working. When I run the code, I get this error:

--Uncaught TypeError: data.allItems[type].push is not a function--

var data = {
    allItems: {
      exp: [],
      inc: []
    },
    totals: {
      exp: 0,
      inc: 0
    },
    budget: 0,
    percentage: -1
  };

  return {
      addItem: function(type, des, val) {
          var newItem, ID;

          // Create new ID
          if (data.allItems[type].length > 0) {
              ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
          } else {
              ID = 0;
          }

          // Create new item based on 'inc' or 'exp' type
          if (type === 'exp') {
              newItem = new Expense(ID, des, val);
          } else if (type === 'inc') {
              newItem = new Ine(ID, des, val);
          }

          // Push it into our data structure
          data.allItems[type].push(newItem);

          // Return the new element
          return newItem;
      },
Share Improve this question edited Aug 19, 2019 at 14:19 Agi Hammerthief 2,1341 gold badge22 silver badges40 bronze badges asked Aug 19, 2019 at 14:11 n.kilanin.kilani 411 gold badge2 silver badges8 bronze badges 4
  • 2 can you add the output of console.log(type) right before you push ? – Taki Commented Aug 19, 2019 at 14:14
  • 2 This might make it easier for others to help: How do I create a runnable stack snippet? – Scott Sauyet Commented Aug 19, 2019 at 14:16
  • In addition to @Taki's ment, what does console.log((typeof type)); return? Is it possible that type is treated as a keyword/reserved word by the JS engine? – Agi Hammerthief Commented Aug 19, 2019 at 14:17
  • 2 Is this an assignment from a school or online coding site? There are several questions with the exact same code on SO stackoverflow./search?q=exp+inc+allItems&mixed=0 – adiga Commented Aug 19, 2019 at 14:28
Add a ment  | 

3 Answers 3

Reset to default 4

You need to first check if your desired array is present in your object. If so, then push to it.

if(data.allItems[type] && Array.isArray(data.allItems[type])) {
    data.allItems[type].push(newItem);
} else {
  console.warn(type + " is undefined in `allItems`!");
}

// OR

if (data.allItems[type] == undefined || !Array.isArray(data.allItems[type])) {
  // Your Error handler
}

Notice: You can only perform push() and pop() methods on an array.

const finalArray = {};
finalArray.push({ id: 1}) // You will definitely get an error.

data.allItems[type] - is undefined in your case , you can use push only for Arrays.

You are incorrectly referencing the array to which you are trying to push.

I would look at writing an if statement to reference the array to which you want to push and then pass an argument to the function call.

var data = {
    allItems: {
      exp: [],
      inc: []
    },
    totals: {
      exp: 0,
      inc: 0
    },
    budget: 0,
    percentage: -1
  };

function hi(type) {
  var test;
  if (type === "exp") {
       test = data.allItems.exp;
      } else {
       test = data.allItems.inc;
  }

  test.push('hi');
  console.log(test);

}

hi('exp');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742296635a4417256.html

相关推荐

  • javascript - TypeError: push() is not a function - Stack Overflow

    I'm trying to push an item to an array but it's not working. When I run the code, I get this

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信