Home > Software design >  With javascript, please tell me how to get result array like below "resultList". It is sim
With javascript, please tell me how to get result array like below "resultList". It is sim

Time:08-11

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 */

CodePudding user response:

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));

CodePudding user response:

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 }

  • Related