У меня есть три многомерных массива с объектами внутри и 4 функций пересчета массива.
function fun_1(val_1,val_2,val_3){
return val_1 + val_2 - val_3
}
function fun_2(val_1,val_2,val_3){
return val_1 + val_2 + val_3
}
function fun_3(val_1,val_2,val_3){
return val_1 - val_2 + val_3
}
function fun_4(val_1,val_2,val_3){
return val_1 - val_2 - val_3
}
arr_1 =[
{ x: 20, y: 20, moveTo: true },
{ x: 70, y: 20 },
{ x: 70, y: 40 },
{ curve: {type: "cubic", x1:10, y1:20, x2:30, y2:5},
x: 3,
y: 4
},
{ x: 90, y: 30 },
{ x: 50, y: 30 },
{ x: 50, y: 50 },
{
curve: {type: "cubic", x1:50, y1:20, x2:10, y2:4},
x: 45,
y: 40
},
{ x: 40, y: 30 },
{ x: 20, y: 20 }
];
arr_2 =[
{ x: 11, y: 65, moveTo: true },
{ x: 44, y: 43 },
{ x: 70, y: 33 },
{ x: 31, y: 2 },
{ curve: {type: "cubic", x1:10, y1:20, x2:30, y2:5},
x: 3,
y: 4
},
{ x: 530, y: 30 },
{ x: 2, y: 150 },
{ x: 253, y: 233 },
{ x: 212, y: 545 },
{
curve: {type: "cubic", x1:50, y1:20, x2:10, y2:4},
x: 45,
y: 40
}
];
arr_3 = [
{ x: 54, y: 123, moveTo: true },
{ x: 44, y: 41 },
{ x: 56, y: 14 },
{ x: 7, y: 2 },
{ x: 11, y: 2 },
{
curve: {type: "cubic", x1:1, y1:4, x2:5, y2:66},
x: 33,
y: 11
},
{ x: 55, y: 33 },
{ x: 66, y: 77 },
{ x: 334, y: 211 },
{
curve: {type: "cubic", x1:7, y1:20, x2:5, y2:4},
x: 45,
y: 13
}
];
Мне нужно пересчитать весь массив и после создать массив с новыми данными.
Я пытался сделать это таким способом
result = []
var type = 1;
$.each(arr_1, function( index, val_1 ) {
result_1 = []
var val_2 = arr_2[index]
var val_3 = arr_3[index]
//Проверка существования curve
curve_check_1 = "curve" in val_1;
curve_check_2 = "curve" in val_2;
curve_check_3 = "curve" in val_3;
if (curve_check_1 || curve_check_2 || curve_check_3){
//Подгон
cur = {type: "cubic", x1:0, y1:0, x2:0, y2:0}
if((curve_check_3 || curve_check_2) && !curve_check_1){
val_1['curve'] = cur;
}else if((curve_check_3 || curve_check_1) && !curve_check_2){
val_2['curve'] = cur;
} else if((curve_check_1 || curve_check_2) && !curve_check_3){
val_3['curve'] = cur;
}
result_1['curve']= [];
result_1['curve']['type'] = "cubic";
if(type == 1){
result_1['curve']['x1'] = fun_1(val_1['curve']['x1'] , val_1['curve']['x1'], val_1['curve']['x1']);
result_1['curve']['y1'] = fun_1(val_1['curve']['y1'] , val_1['curve']['y1'], val_1['curve']['y1']);
result_1['curve']['x2'] = fun_1(val_1['curve']['x2'] , val_1['curve']['x2'], val_1['curve']['x2']);
result_1['curve']['y2'] = fun_1(val_1['curve']['y2'] , val_1['curve']['y2'], val_1['curve']['y2']);
} else if (type == 2){
result_1['curve']['x1'] = fun_2(val_1['curve']['x1'] , val_1['curve']['x1'], val_1['curve']['x1']);
result_1['curve']['y1'] = fun_2(val_1['curve']['y1'] , val_1['curve']['y1'], val_1['curve']['y1']);
result_1['curve']['x2'] = fun_2(val_1['curve']['x2'] , val_1['curve']['x2'], val_1['curve']['x2']);
result_1['curve']['y2'] = fun_2(val_1['curve']['y2'] , val_1['curve']['y2'], val_1['curve']['y2']);
} else if (type == 3){
result_1['curve']['x1'] = fun_3(val_1['curve']['x1'] , val_1['curve']['x1'], val_1['curve']['x1']);
result_1['curve']['y1'] = fun_3(val_1['curve']['y1'] , val_1['curve']['y1'], val_1['curve']['y1']);
result_1['curve']['x2'] = fun_3(val_1['curve']['x2'] , val_1['curve']['x2'], val_1['curve']['x2']);
result_1['curve']['y2'] = fun_3(val_1['curve']['y2'] , val_1['curve']['y2'], val_1['curve']['y2']);
} else if (type == 4){
result_1['curve']['x1'] = fun_4(val_1['curve']['x1'] , val_1['curve']['x1'], val_1['curve']['x1']);
result_1['curve']['y1'] = fun_4(val_1['curve']['y1'] , val_1['curve']['y1'], val_1['curve']['y1']);
result_1['curve']['x2'] = fun_4(val_1['curve']['x2'] , val_1['curve']['x2'], val_1['curve']['x2']);
result_1['curve']['y2'] = fun_4(val_1['curve']['y2'] , val_1['curve']['y2'], val_1['curve']['y2']);
}
}
if(type == 1){
result_1['x'] = fun_1(val_1['x'] , val_2['x'], val_3['x']);
result_1['y'] = fun_1(val_1['y'] , val_2['y'], val_3['y']);
} else if (type == 2){
result_1['x'] = fun_2(val_1['x'] , val_2['x'], val_3['x']);
result_1['y'] = fun_2(val_1['y'] , val_2['y'], val_3['y']);
} else if (type == 3){
result_1['x'] = fun_3(val_1['x'] , val_2['x'], val_3['x']);
result_1['y'] = fun_3(val_1['y'] , val_2['y'], val_3['y']);
} else if (type == 4){
result_1['x'] = fun_4(val_1['x'] , val_2['x'], val_3['x']);
result_1['y'] = fun_4(val_1['y'] , val_2['y'], val_3['y']);
}
result.push(result_1);
});
console.log(arr_1);
console.log(result);
Но почему то у меня ни как не получается сделать это правильно.
Кто нибудь может мне помочь с этим?