fredi tansari
fredi tansari

Reputation: 87

Best practice to add new object to javascript

I need the best practices for advice. Let's Assume I have a trucks object like this:

    var trucks = [
    {truckId:"1", Manufacturer:"Toyota", Type:"Tacoma", NoSeries:"1",mileage:"5000" },
    {truckId:"2", Manufacturer:"Ford", Type:"F-150", NoSeries:"2",mileage:"7644"},
    {truckId:"5", Manufacturer:"Geely", Type:"Bear", NoSeries:"5", mileage:"156778"},
    {truckId:"3", Manufacturer:"Nissan", Type:"Pathfinder", NoSeries:"3", mileage:"6722"},
    {truckId:"4", Manufacturer:"Tesla", Type:"Cybertruck", NoSeries:"4", mileage:"999999"},
 ];

and I want to add this new maintenance record object:

 let newmaintenanceRecord = 
[
    {complains: "Broken AC", action: "Fixed Leaky Condensor"}, 
    {complains: "sounds like a hammer pounding midget in the trunk", action: "took hammer away from midget"}, 
]

into the truckId#5 object.

I tried these lines on the project but it says the trucks are not iterable,

    let index = trucks.findIndex(truck => truck.truckId==="5");
    trucks = [...trucks[index], newmaintenanceRecord];
    console.log(trucks);

These lines erased all trucks object except the new record

trucks = [trucks[index],...newmaintenanceRecord];

Upvotes: 1

Views: 80

Answers (4)

AyushKatiyar
AyushKatiyar

Reputation: 1030

Try using

trucks.forEach((truck)=>{
if(truck.truckId === '5'){
    truck.maintenanceRecord = newmaintenanceRecord;
   }
}

Upvotes: 0

bgazsi
bgazsi

Reputation: 141

After you found the index of the truck with: let index = trucks.findIndex(truck => truck.truckId==="5");, you can modify this certain object: trucks[index].maintenence = newmaintenanceRecord

Upvotes: 2

user_3737282893
user_3737282893

Reputation: 21

You wouldn't overcomplicate your code and simply add a property to the object

const trucks = [
{truckId:"1", Manufacturer:"Toyota", Type:"Tacoma", NoSeries:"1",mileage:"5000" },
{truckId:"2", Manufacturer:"Ford", Type:"F-150", NoSeries:"2",mileage:"7644"},
{truckId:"5", Manufacturer:"Geely", Type:"Bear", NoSeries:"5", mileage:"156778"},
{truckId:"3", Manufacturer:"Nissan", Type:"Pathfinder", NoSeries:"3", mileage:"6722"},
{truckId:"4", Manufacturer:"Tesla", Type:"Cybertruck", NoSeries:"4", mileage:"999999"},
];

const newmaintenanceRecord = 
[
    {complains: "Broken AC", action: "Fixed Leaky Condensor"}, 
    {complains: "sounds like a hammer pounding midget in the trunk", action: "took hammer away from midget"}, 
]


function addMaintainance(trucks, truckId, maintainanceRecords) {
  const truck5 = trucks.find(({truckId: id}) => id === truckId);
  if (truck5) {
    truck5.maintainance = (truck5.maintainance || []).concat(...maintainanceRecords);
  }
  return trucks;
}

console.log(addMaintainance(trucks, "5", newmaintenanceRecord));

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370829

The problem is

[ ...trucks[index],

But trucks[index] is an object, not an array, so the object can't be spread into an array.

If you want to change the object at that index to add the new property, use .find to get a reference to the object, then assign a new property to it, containing the newmaintenanceRecord:

const truck = trucks.find(truck => truck.truckId==="5"));
if (truck) {
  truck.newmaintenanceRecord = newmaintenanceRecord;
}

Upvotes: 1

Related Questions