Home > Software design >  Javascript/ NodeJS - Map two nested arrays and output result if any and NULL if not
Javascript/ NodeJS - Map two nested arrays and output result if any and NULL if not

Time:07-29

I get two nested arrays, like this:

let actual = [["123", "short_text"], ["456", "short_text"], ["789", "varchar"], ["101", "long_text"], ["112", "short_text"]];
let values = [["789", "blabla"], ["456", "lala"]];

Actual - the potential fields in a database [ref_id, field_type]. Actual is the correct structure of the test - always.

Values - a source with values from a test result [ref_id, test_answer].

I would like to match values ref_id to actual ref_id, output the answer if any and print "NULL" if no answer is given.

Expected result:

let controlledArray = [
    {id: "123", val: "NULL"},
    {id: "456", val: "lala"},
    {id: "789", val: "blabla"},
    {id: "101", val: "NULL"},
    {id: "112", val: "NULL"},
];

Have tried this (didn't work):

let actual = [["456", "short_text"], ["789", "varchar"], ["101", "long_text"], ["112", "short_text"]];
let values = [["789", "blabla"], ["456", "lala"]];  

let map = {};
actual.forEach(i => map[i] = "NULL");
values.forEach(i => map[i] === "NULL" && (map[i] = i));
let controlledArray = Object.keys(map).map(k => ({ id: k, val: map[k] }));

console.log(controlledArray)

Guess this is fairly simple, but I have just got stuck on this - help appreciated

CodePudding user response:

Using Object.fromEntries with map and Destructuring assignment

let actual = [
  ["123", "short_text"],
  ["456", "short_text"],
  ["789", "varchar"],
  ["101", "long_text"],
  ["112", "short_text"]
];
let values = [
  ["789", "blabla"],
  ["456", "lala"]
];
let obj = Object.fromEntries(values); //{456: 'lala', 789: 'blabla'}
let result = actual.map(arrs => arrs[0]) //  ['123', '456', '789', '101', '112']
  .map(id => ({
    id,
    val: obj[id] ?? 'NULL'
  }))
console.log(result);

Notice operator ?? is nullish coalescing operator means if value is null or undefined returns its right hand value, in our case 'NULL'

CodePudding user response:

I'd also create a temporary map object, but one that is keyed by the references and has corresponding values that have the target structure with "NULL". Then iterate the second array to modify some "NULL" to actual values. Finally extract the values from the temporary map object:

const actual = [["123", "short_text"], ["456", "short_text"], ["789", "varchar"], ["101", "long_text"], ["112", "short_text"]];
const values = [["789", "blabla"], ["456", "lala"]];

const map = Object.fromEntries(actual.map(([id]) => [id, {id, val: "NULL"}]));
values.forEach(([id, val]) => map[id].val = val);
const controlledArray = Object.values(map);

console.log(controlledArray);

CodePudding user response:

let actual = [["123", "short_text"], ["456", "short_text"], ["789", "varchar"], ["101", "long_text"], ["112", "short_text"]];
let values = [["789", "blabla"], ["456", "lala"]];

const valuesMap = {}
values.forEach(([k, v]) => (valuesMap[k] = v))
const controlledArray = actual.map(([k, v]) => ({ [k]: valuesMap[k] || 'NULL' }))

CodePudding user response:

You can create an object from the values array for lookup. Then lookup the identifier from the object or set "NULL" as default if it doesn't exist.

let actual = [["123", "short_text"], ["456", "short_text"], ["789", "varchar"], ["101", "long_text"], ["112", "short_text"]];
let values = [["789", "blabla"], ["456", "lala"]];


const map = Object.fromEntries(values);
const controlledArray = actual.map(
  ([id, val]) => ({id, val: map[id] || "NULL"})
);
console.log(controlledArray);

  • Related