i want to build new list of objects with average value from another list of objects The original list :
original_list = [{"time":16485,"max_count":6,"min_count":4,"max_ca":10,"min_ca":8},{"time":16495,"max_count":12,"min_count":10,"max_ca":8,"min_ca":6},]
The new list i want to get
new_list = [{"time":16485,"average_count":5,"average_ca":9},{"time":16495,"average_count":11,"average_ca":7,]
I tried this code but it doesn't work
const new_list = [];
original_list.map((element) => { 'time': element.time, 'average_count':
$(element.max_count)/2 + $(element.min_count)/2, 'average_ca': (element.max_ca)/2 + $(element.min_ca)/2 });
but i got lot of syntax error, any idea how i can get the result please
You need to wrap the function body in parentheses. It should look like this:
const new_list = original_list.map((element) => ({ 'time': element.time, 'average_count': $(element.max_count)/2 + $(element.min_count)/2, 'average_ca': (element.max_ca)/2 + $(element.min_ca)/2 }));
Here are the docs on arrow functions
I'd do something like this:
const new_list = original_list.map((element) => ({
time: element.time,
average_count: (element.max_count + element.min_count) / 2,
average_ca: (element.max_ca + element.min_ca) / 2,
}));