0

Original Code

    let base = {
        brand: 'Ford',
        model: '556',
        licensePlate: 55554,
    };
    
    let carsObj = {};
    let carsArr = [];
    
    for (let i = 1; i < 6; i++) {
        carsObj[`car${i}`] = {
            brand: base.brand,
            model: base.model,
            licensePlate: base.licensePlate + i
        }
        carsArr.push({
            brand: base.brand,
            model: base.model,
            licensePlate: base.licensePlate + i
        });;
    };

Logs

    {
      car1: { brand: 'Ford', model: '556', licensePlate: 55555 },
      car2: { brand: 'Ford', model: '556', licensePlate: 55556 },
      car3: { brand: 'Ford', model: '556', licensePlate: 55557 },
      car4: { brand: 'Ford', model: '556', licensePlate: 55558 },
      car5: { brand: 'Ford', model: '556', licensePlate: 55559 }
    }

Now How can I remove car2 and car4 from the carsObj, using it's Index location like [0] and [3], instead of delete carsObj.car2 and then store it in newCarsObj so it logs the following:

    {
      car1: { brand: 'Ford', model: '556', licensePlate: 55555 },
  
      car3: { brand: 'Ford', model: '556', licensePlate: 55557 },
    
      car5: { brand: 'Ford', model: '556', licensePlate: 55559 }
    }

not this answer in link - as this is just an object with property+value. and not an object within object Looking for a solution in JavaScript (without jQuery , lodash etc) - thanks in advance.

  • You cannot do that; properties don't have an "index", they've just got their name. – Pointy Mar 16 '21 at 11:32
  • 1
    You could use `Object.keys()` to get an array of property names, but making the assumption that a particular index into that array will definitely correspond to a particular property is a very fragile idea. – Pointy Mar 16 '21 at 11:33

1 Answers1

-1

This is what you need.

 let obj = {
  car1: { brand: 'Ford', model: '556', licensePlate: 55555 },
  car2: { brand: 'Ford', model: '556', licensePlate: 55556 },
  car3: { brand: 'Ford', model: '556', licensePlate: 55557 },
  car4: { brand: 'Ford', model: '556', licensePlate: 55558 },
  car5: { brand: 'Ford', model: '556', licensePlate: 55559 }
}

let removeItems = (arr) => {
  arr.map(x => {
    return Object.keys(obj)[x]
  }).forEach(x => delete obj[x])
}

removeItems([1, 2])

console.log(obj)
Bulent
  • 3,307
  • 1
  • 14
  • 22
  • Property order is not guaranteed so this is hit or miss at best. see: [Does JavaScript guarantee object property order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – pilchard Mar 16 '21 at 12:29
  • If you look at the code that creates the obj in the first place, you can see that the properties are ordered. So my solution will work unless it's called twice for the same object. – Bulent Mar 16 '21 at 12:34
  • *'...unless it's called twice for the same object.'* Declaring a function that can only be called once on a single object and then only when the exact shape of the object is known is extremely fragile. – pilchard Mar 16 '21 at 12:41
  • That is not my concern. I'm sure he/she will realize that issue soon enough and try to find a better solution. – Bulent Mar 16 '21 at 12:44