0

I need to get address, lat and lng:

let Orders= [{
  pedido: this.listAddress[0].address,
  lat: this.listAddress[0].lat,
  lng: this.listAddress[0].lng
}]

But in that way, i only get the first item, i need to get all datas dynamically

0: {address:"test 00", lat:"00", lng:"00"}
1: {address:"test 01", lat:"01", lng:"01"}
2: {address:"test 02", lat:"01", lng:"02"}
3: {address:"test 03", lat:"01", lng:"03"}
2
  • Arrays don't have keys, objects do. What you have is a single object in an array. If there will only be one object, the array is an unnecessary wrapper. And where is the code for listAddress? Commented Aug 10, 2021 at 4:15
  • What is your input? Commented Aug 10, 2021 at 4:15

6 Answers 6

2
const orders = listAddress.map(list => {
   return ({
      pedido: list.address,
      lat: list.lat,
      lng: list.lng
   })
})
Sign up to request clarification or add additional context in comments.

Comments

0

not sure if you're looking for something like below. Take a look

let Orders = this.listAddress.map(la => ({
    address: la.address,
    lat: la.lat,
    lng: la.lng
}));

Comments

0

Assuming listAddress is an array of object, you need to iterate over the array in order to get every element


let order = listAddress.map(el => {
 return {
      pedido: el.address,
      lat: el.lat,
      lng: el.lng
   }
})

1 Comment

Running .map over array and pushing items in different array variable is wrong practice. .map it self returns an array, so just storing returned output in new variable will be enough.
0

const order = {
    0: {address:"test 00", lat:"00", lng:"00"},
    1: {address:"test 01", lat:"01", lng:"01"},
    2: {address:"test 02", lat:"01", lng:"02"},
    3: {address:"test 03", lat:"01", lng:"03"},
}


Object.values(order).forEach(obj =>{
    const {address, lat, lng} = obj;
    console.log(address, lat, lng)
})

Comments

0

You may try Array.map method in JS

So the usage is like this

const inputArray = [
   {address:"test 00", lat:"00", lng:"00"},
   {address:"test 01", lat:"01", lng:"01"},
   {address:"test 02", lat:"02", lng:"02"},
]

const outputArray = inputArray.map( item => (
      {
         pedido: item.address,
         lat: item.lat,
         lng: item.lng,
      }
   )
)

console.log(outputArray) // [Object { pedido: "test 00", lat: "00", lng: "00" }, Object { pedido: "test 01", lat: "01", lng: "01" }, Object { pedido: "test 02", lat: "02", lng: "02" }]

Learn more here Array.prototype.map()

Comments

0
const orders = listAddress.map(list => {
   return ({
     pedido: list.address,
     lat: list.lat,
     lng: list.lng
  })
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.