How to add properties inside an object with prompt in JavasScript?

Viewed 91

I would like to add more properties inside this object:

let flights = 
[
    { id: 00, to: 'Bilbao', from: 'Barcelona', cost: 1600, scale: false },
    { id: 01, to: 'New York', from: 'Barcelona', cost: 700, scale: false },
    { id: 02, to: 'Los Angeles', from: 'Madrid', cost: 1100, scale: true },
    { id: 03, to: 'Paris', from: 'Barcelona', cost: 210, scale: false },
    { id: 04, to: 'Roma', from: 'Barcelona', cost: 150, scale: false },
    { id: 05, to: 'London', from: 'Madrid', cost: 200, scale: false },
    { id: 06, to: 'Madrid', from: 'Barcelona', cost: 90, scale: false },
    { id: 07, to: 'Tokyo', from: 'Madrid', cost: 1500, scale: true },
    { id: 08, to: 'Shangai', from: 'Barcelona', cost: 800, scale: true },
    { id: 09, to: 'Sydney', from: 'Barcelona', cost: 150, scale: true },
    { id: 10, to: 'Tel-Aviv', from: 'Madrid', cost: 150, scale: false } 
];

What I did is create a variable that stores the data you want to add an then I push it inside flights, like this:

  var newFlight = prompt('Introduce the fligth information:');

        if (newFlight) {
            var obj = new Object(newFlight);

            flights.push(obj);
            
            console.log(flights);
        }

What I obtain is that:

enter image description here

And then inside the object we have that:

enter image description here

How can I obtain this:

{id: 11, to: "Australia", from: "Madrid", cost: 1000, scale: true}

instead of this:

String {"11,Australia,Madrid,1000,true"}

2 Answers

Split the string using String.prototype.split()

let flights = 
[
    { id: 00, to: 'Bilbao', from: 'Barcelona', cost: 1600, scale: false },
    { id: 01, to: 'New York', from: 'Barcelona', cost: 700, scale: false },
    { id: 02, to: 'Los Angeles', from: 'Madrid', cost: 1100, scale: true },
    { id: 03, to: 'Paris', from: 'Barcelona', cost: 210, scale: false },
    { id: 04, to: 'Roma', from: 'Barcelona', cost: 150, scale: false },
    { id: 05, to: 'London', from: 'Madrid', cost: 200, scale: false },
    { id: 06, to: 'Madrid', from: 'Barcelona', cost: 90, scale: false },
    { id: 07, to: 'Tokyo', from: 'Madrid', cost: 1500, scale: true },
    { id: 08, to: 'Shangai', from: 'Barcelona', cost: 800, scale: true },
    { id: 09, to: 'Sydney', from: 'Barcelona', cost: 150, scale: true },
    { id: 10, to: 'Tel-Aviv', from: 'Madrid', cost: 150, scale: false } 
];

var newFlight = prompt('Introduce the fligth information:');

if (newFlight) {
  const values = newFlight.split(",");
  flights.push({
    id: parseInt(values[0]),
    to: values[1],
    from: values[2],
    cost: parseInt(values[3]),
    scale: values[4] === "true"
  });
  console.log(flights);
}

split the string at the commas and assign the parts to properties of a new object:

let [id, to, from, cost, scale] = newFlight.split(',')
let obj = {
  id: parseInt(id),
  to: to,
  from: from,
  cost: parseInt(cost),
  scale: scale == 'true'
}
Related