-2

I am getting the error forEach is not a function. Can anyone please help? Basically i want to create a shallow copy of filteredSettlements and use that for iteration. Here is the code snippet for more details

const test = $.extend({}, test1);
test.forEach((test1) => {
 //anything
 });
user3681970
  • 1,141
  • 3
  • 17
  • 35
  • 3
    You're creating a new object, not an array. Objects do not have `forEach`. Did you mean `$.extend([], ...`? Or you can `Object.entries` or something over the object, if you want. – CertainPerformance Apr 25 '18 at 07:47
  • In simple words - filteredSettlementsForIteration is not an array. Foreach is applied to an array. Here you are looping through objects hence Foreach will not work as you are extending to objects. You can try using simple for loop. – Amit Kulat Apr 25 '18 at 09:00

2 Answers2

2

You are extending filteredSettlements into an Object {}. And object doesnt have forEach Method.

Array has forEach method

sridhar..
  • 1,632
  • 12
  • 18
  • Yeah.. Got it. I think I should use slice to create the shallow copy of parent array : https://stackoverflow.com/questions/47738344/does-javascript-slice-method-return-a-shallow-copy?rq=1 – user3681970 Apr 25 '18 at 07:49
  • @user3681970 you can do `$.extend([],` instead of `$.extend({},` – Anant Kumar Singh Apr 25 '18 at 07:50
0

I assume filteredSettlements is an array. Try this

const filteredSettlementsForIteration = filteredSettlements.slice();
filteredSettlementsForIteration.forEach(settlement => {});
soyelmnd
  • 55
  • 7