Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
JavaScript Merging Objects into New Array from API

So I'm pulling two arrays from an API for a Vue.JS DataTable, one of the arrays contains and object with information for the table about a specific devices, such as names, IDs, dates, and etc., whereas the other array contains an object with multiple statuses, such as "lost", "noSignal", and etc.

I need to have these arrays merged and inputted into a new object, which will then be stored into a new Array for the DataTable:

let data = {
 name: name,
 IDs: id,
 date: date
 warning: null,
 error: null
}

The array with statuses, array 2, dictate the values of "warning" and "error", and I need to loop through and input each "data" object into a new array with each individual warning or error, so if "lowSignal" AND "lost" are both true, then the data table will have two entries, one where the warning column is populated with "lowSignal" and the error column has nothing, and another where the error column is populated with "lost" and warning has nothing in it.

Here is what I've tried: Having multiple if conditions for each possible error, such as if(lowSignal), if(lost), and etc., with each if statement containing a new variable set to represent the data object, let temp = data;, then I would configure and merge: temp.warning = lowSignal, merge.push[temp] This did not work, as if both lowSignal AND lost were true, it would input two entries into the DataTable (which is correct), but both entries would have warning and error populated with the same thing (incorrect). I've also attempted to have a .forEach loop, as well as an object length loop, both returning the same result.

Any suggestions? Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use the Spread Operator to merge two objects. If you have a property in one array related to a property in the other array (device.id and status.deviceId), you can do something similar to this:

const devices = [
    {
        id: 1,
        name: 'Device A',
    },
    {
        id: 2,
        name: 'Device B',
    },
    {
        id: 3,
        name: 'Device C',
    },
    {
        id: 4,
        name: 'Device D',
    },
]

const statuses = [
    {
        id: 1,
        deviceId: 1,
        warning: 'low signal',
        error: 'lost'
    },
    {
        id: 2,
        deviceId: 2,
        warning: 'low signal',
        error: null
    },
    {
        id: 2,
        deviceId: 3,
        warning: null,
        error: 'lost'
    },
    {
        id: 2,
        deviceId: 3,
        warning: null,
        error: null
    }
]

const devicesWithStatus = devices.reduce((acc, device) => {
    // Find the status where deviceId equals to the ID of the device
    const status = statuses.find((status) => status.deviceId === device.id)
    
    // If there's a warning, merge the device and the warning
    if(status?.warning)
      acc.push({
        ...device,
        warning: status.warning
      })

    // If there's an error, merge the device and the error
    if(status?.error)
      acc.push({
        ...device,
        error: status.error
      })
     
    // If there's no error or warning, just push the device
    if(!status?.error && !status?.warning)
      acc.push(device)

    return acc
}, [])

console.log(devicesWithStatus)

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understood you correctly try like following snippet:

const arr = [{id: 0, name: 'a', desc: 'ddd'}, {id: 1, name: 'b', desc: 'ddd'}, {id: 2, name: 'c', desc: 'ddd'}, {id: 3, name: 'd', desc: 'ddd'}]
const stat = [{id: 0, lost: true, lowSignal: true}, {id: 1, lost: false, lowSignal: true}, {id: 2, lost: true, lowSignal: false}]

const res = []
arr.forEach(a => {
  let st = stat.find(s => s.id === a.id)
  let newObj = {
    id: a.id,
    name: a.name,
    error: null,
    warning: null
  }
  if(st) {
    if(st.lost) {
      res.push({ ...newObj, error: 'lost' })
    } 
    if (st.lowSignal) {
      res.push({ ...newObj, warning: 'lowSignal' })
    }
  } else {
    res.push({ ...newObj })
  }
})

console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda