I have the following array (name is always random)
array =
[
{
active: true,
field1: "100",
field2: "",
field3: "1",
name: 0.0020123,
ventasAnuales: "ventasAnuales",
slActive: true
},
{
active: true,
field1: "100",
field2: "",
field3: "1",
name: 0.0020123,
ventasAnuales: "ventasAnuales",
slActive: true
},
{
active: true,
field1: "100",
field2: "",
field3: "1",
name: 0.0020123,
ventasAnuales: "ventasAnuales",
slActive: true
},
{
active: true,
field1: "100",
field2: "",
field3: "1",
name: 0.0020123,
empleados: "empleados",
slActive: true
}
]
Now I want to add extrao objects to the array, and I do it with a concat
array.concat(a).filter(i => i.active)
Problem is that there are certains conditions I should look for before doing the concat. Like for example
- If an object with equal field1 and has the same last but one property
ventasAnuales | empleadosalready exists in the array, but field3 is different. It should replace it
For example, given that I want to add the following object
{
active: true,
field1: "100",
field2: "",
field3: "2",
name: 0.0020423,
ventasAnuales: "ventasAnuales",
slActive: true
},
It would replace the first element of the array
- If i add an object with the same field3 value, the same last but one property
ventasAnuales | empleadosand the field1 is different, it should be replaced by the new object.
For example, given that i want to add the following object.
{
active: true,
field1: "50",
field2: "",
field3: "1",
name: 0.1020123,
ventasAnuales: "ventasAnuales",
slActive: true
}
It should end up replacing the first element of the array.
How should i write my concat to make this work?