0

My code is done in angular.js and I am trying to add several elements to the array. something like that:

$scope.my_array=[];
$scope.my_array.push({"element":1});
var data= {"element":2},{"element":3}
$scope.my_array.push(...data);
console.log($scope.my_array) => [{"element":1},{"element":2},{"element":3}]

I am developing in ionic, and in modern cell phones it works. but in cell phones with versions of 4.4 it does not work and this error appears.

"Uncaught SyntaxError: Unexpected token ." 

What alternative can I do using good practice?

4
  • Looks like spread operator is not supported. Check the version of the browser and support for spread operator. Commented Jun 24, 2018 at 2:17
  • ok, but what good practice can I follow, what do you advise me, I would not like to do a cycle "for", maybe there is a better way Commented Jun 24, 2018 at 2:22
  • use Babel if you need to support new Javascript syntax for old browsers Commented Jun 24, 2018 at 2:23
  • Did you try to remove the syntax error for data? Commented Jun 24, 2018 at 9:47

1 Answer 1

1

As mentioned in the comment by Barun, the ... spread syntax isn't supported. Use Array.concat to append the elements from data array to my_array array:

$scope.my_array=[];
$scope.my_array.push({"element":1});
var data= [{"element":2},{"element":3}];
$scope.my_array = $scope.my_array.concat(data);
console.log($scope.my_array);

For more information on JavaScript spread syntax:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.