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

189
Visualizações
With javascript, please tell me how to get result array like below "resultList". It is similar to the UPDATE statement in SQL

With javascript, please tell me how to get result array like below "resultList". It is similar to the UPDATE statement in SQL.

const dataList = [
  { id: 1, xpstn: "10", ypstn: "11", value:{1,2,3,4}},
  { id: 2, xpstn: "20", ypstn: "21", value:{1,2,3,4}},
  { id: 3, xpstn: "30", ypstn: "31", value:{1,2,3,4}},
  { id: 4, xpstn: "40", ypstn: "41", value:{1,2,3,4}},
     ]
 const paramsList = [
  { id: 3, xpstn: "33", ypstn: "32", desc: "param"},
  { id: 4, xpstn: "44", ypstn: "42", desc: "param"},
  { id: 5, xpstn: "55", ypstn: "53", desc: "param"},
  { id: 6, xpstn: "66", ypstn: "62", desc: "param"},
];


let resultList = [
  { id: 3, xpstn: "33", ypstn: "11", value:{1,2,3,4}},
  { id: 4, xpstn: "44", ypstn: "21", value:{1,2,3,4}},
];

The condition is that the id is the same, and xpstn must be updated. /* UPDATE X SET X.xpstn = Y.xpstn FROM dataList X INNER JOIN paramsList Y
ON X.id = Y.id */

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

0

hope this works for you.

const dataList = [
    { id: 1, xpstn: "10", ypstn: "11", value: [1, 2, 3, 4] },
    { id: 2, xpstn: "20", ypstn: "21", value: [1, 2, 3, 4] },
    { id: 3, xpstn: "30", ypstn: "31", value: [1, 2, 3, 4] },
    { id: 4, xpstn: "40", ypstn: "41", value: [1, 2, 3, 4] },
]
const paramsList = [
    { id: 3, xpstn: "33", ypstn: "32", desc: "param" },
    { id: 4, xpstn: "44", ypstn: "42", desc: "param" },
    { id: 5, xpstn: "55", ypstn: "53", desc: "param" },
    { id: 6, xpstn: "66", ypstn: "62", desc: "param" },
];


let resultList = [
    { id: 3, xpstn: "33", ypstn: "31", value: [1, 2, 3, 4] },
    { id: 4, xpstn: "44", ypstn: "41", value: [1, 2, 3, 4] },
];

/*
UPDATE X
SET X.xpstn = Y.xpstn
FROM dataList X
INNER JOIN paramsList Y  
ON X.id = Y.id
*/

const update = (data = [], params = []) => {
    resultList = [];
    data = data.forEach(d => {
        if (params.some(p => p.id === d.id)) {
            r = {
                ...d,
                xpstn: params.find(p => p.id === d.id).xpstn
            }
            resultList.push(r);
        }
    });
    return resultList;
}

console.log("result", update(dataList, paramsList));

about 4 years ago · Juan Pablo Isaza Relatório

0

Solution with reduce:

const dataList = [{ id: 1, xpstn: "10", ypstn: "11", value: [1, 2, 3, 4] },{ id: 2, xpstn: "20", ypstn: "21", value: [1, 2, 3, 4] },{ id: 3, xpstn: "30", ypstn: "31", value: [1, 2, 3, 4] },{ id: 4, xpstn: "40", ypstn: "41", value: [1, 2, 3, 4] }];
const paramsList = [{ id: 3, xpstn: "33", ypstn: "32", desc: "param" },{ id: 4, xpstn: "44", ypstn: "42", desc: "param" },{ id: 5, xpstn: "55", ypstn: "53", desc: "param" },{ id: 6, xpstn: "66", ypstn: "62", desc: "param" }];

const resultList = dataList.reduce((acc, dataItem) => {
    const paramItem = paramsList.find(({ id }) => id === dataItem.id);
    if (paramItem) {
        const { xpstn } = paramItem;
        acc.push({ ...dataItem,  xpstn });
    }
    return acc;
}, []);

console.log(resultList)
.as-console-wrapper { max-height: 100% !important; top: 0 }

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