Here first array of object
let arrobj1 = [{
"Expenses": "House Rent",
"Price": "12000",
"timestamp": "1677729781000"
},{
"Expenses": "school Fees",
"Price": "18000",
"timestamp": "1677729781000"
},{
"Expenses": "Electricty Bill",
"Price": "1000",
"timestamp": "1677909781000"
}]
Second array of object
let arrobj2 = [{
"Collection": "Salaries",
"Price": "42000",
"timestamp": "1677747781000"
},{
"Collection": "sell Bike",
"Price": "13000",
"timestamp": "1677729781000"
},{
"Collection": "sell a sofa",
"Price": "1000",
"timestamp": "1677909781000"
},{
"Collection": "sell a milk",
"Price": "4000",
"timestamp": "1677715381000"
}]
convert into single array object result
finalarry = [{
"Expenses": "House Rent",
"Collection" :"",
"Price": "12000",
"Close Amount": "",
"timestamp": "1677729781000"
},{
"Expenses": "school Fees",
"Price": "18000",
"Collection" :"",
"Close Amount": "",
"timestamp": "1677729781000"
},{
"Expenses": "",
"Collection": "Salaries",
"Price": "42000",
"Close Amount": "",
"timestamp": "1677747781000"
},{
"Expenses": "",
"Collection": "sell Bike",
"Price": "13000",
"Close Amount": "",
"timestamp": "1677729781000"
},{
"Expenses": "",
"Collection": "",
"Price": "",
"Close Amount": "25000",
"timestamp": "1677729781000"
}]
day wise calculate collection-expense=closing amount
>Solution :
To merge two arrays of objects into a single array object, you can use the map() method to iterate over each object in both arrays and create a new object with the merged properties. Here’s an example implementation:
let arrobj1 = [
{ "Expenses": "House Rent", "Price": "12000", "timestamp": "1677729781000" },
{ "Expenses": "school Fees", "Price": "18000", "timestamp": "1677729781000" },
{ "Expenses": "Electricty Bill", "Price": "1000", "timestamp": "1677909781000" }
];
let arrobj2 = [
{ "Collection": "Salaries", "Price": "42000", "timestamp": "1677747781000" },
{ "Collection": "sell Bike", "Price": "13000", "timestamp": "1677729781000" },
{ "Collection": "sell a sofa", "Price": "1000", "timestamp": "1677909781000" },
{ "Collection": "sell a milk", "Price": "4000", "timestamp": "1677715381000" }
];
let finalarray = arrobj1.map((item1) => {
let item2 = arrobj2.find((item2) => item2.timestamp === item1.timestamp);
let closingAmount = "";
if (item1.Price && item2 && item2.Price) {
closingAmount = item2.Price - item1.Price;
}
return {
"Expenses": item1.Expenses || "",
"Collection": item2 ? item2.Collection : "",
"Price": item1.Price || "",
"Close Amount": closingAmount,
"timestamp": item1.timestamp
};
});
In this implementation, the map() method is used to iterate over each object in arrobj1, and the corresponding object in arrobj2 is found using the find() method. If a corresponding object is found, the closing amount is calculated by subtracting the Price property of the object in arrobj1 from the Price property of the corresponding object in arrobj2.
The resulting array is an array of objects with the merged properties from both arrays. The Expenses and Collection properties are merged into a single property, and the closing amount is calculated and added as a new property.