Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a function to each object in a JavaScript array

[ {name:'hi',data:'1,2,3,4,5'} , {name:'hello',data:'5,4,3,2,1'} ] 

What I need is to apply a split on data of each object in array so that the result would be:

[ {name:'hi',data:[1,2,3,4,5]} , {name:'hello',data:[5,4,3,2,1]} ] 

I know I can loop through the array using for each and produce a new array, but is there a better, faster method?

var arr = [{     name: 'hi',     data: '1,2,3,4,5' }, {     name: 'hello',     data: '5,4,3,2,1' }];  var new_arr = []; for (i in arr) {     var temp = {};     temp.name = arr[i].name;     temp.data = arr[i].data.split(',');     new_arr.push(temp); } 
like image 945
Zalaboza Avatar asked Apr 21 '15 15:04

Zalaboza


People also ask

Can I put function in array JavaScript?

The simple answer is yes you can place function in an array. In fact, can declare variables and reference them in your function.

Which function of an array object calls a function for each element in the array?

Which function of an Array object calls a function for each element in the array? Explanation: forEach() – Calls a function for each element in the array.

Which array method should you apply to run a function for every item within an array returning an array of all items for which the function is true?

forEach(callback) method is an efficient way to iterate over all array items. Its first argument is the callback function, which is invoked for every item in the array with 3 arguments: item, index, and the array itself.

How do you call a function in an array?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition.


2 Answers

You could use Array.prototype.map:

var new_array = old_array.map(function(e) {    e.data = e.data.split(',');    return e; }); 

As the comment said, this way changes the old_array. You could also return a new object in the callback function without changing the original array.

like image 57
xdazz Avatar answered Oct 16 '22 06:10

xdazz


var data = [{     name: 'hi',     data: '1,2,3,4,5' }, {     name: 'hello',     data: '5,4,3,2,1' }]; 

You can use the Array.prototype.map on data to construct a new Array, like this

var result = data.map(function (currentObject) {     return {         name: currentObject.name,         data: currentObject.data.split(",").map(Number)     }; }); 

Here, we split the currentObject.data based on , and then we call Number function on all the split strings, so that you will get the result object's data as numbers, as you wanted in the question.

Output

[{     name: 'hi',     data: [1, 2, 3, 4, 5] }, {     name: 'hello',     data: [5, 4, 3, 2, 1] }] 

let data = [{     name: 'hi',     data: '1,2,3,4,5'   }, {     name: 'hello',     data: '5,4,3,2,1'   }],   result = data.map(function(currentObject) {     return {       name: currentObject.name,       data: currentObject.data.split(",").map(Number)     };   }); console.log(result);
like image 34
thefourtheye Avatar answered Oct 16 '22 06:10

thefourtheye