Home > Mobile >  JS: Compare and validate two arrays
JS: Compare and validate two arrays

Time:09-16

I have to arrays:

const values = [
  {
    id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
    capacity: 8
  },
  {
    id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
    capacity: 6
  },
 {
    id: "djkfu4-xvvxvx-4344-sddsd",
    capacity: 1
  }
]



const allowedValues = [
  {
    id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
    capacity: 3
  },
  {
    id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
    capacity: 2
  }
]

I need to check if capacity from exact object from values exceeds the one from allowedValues and return an error.

So, based on example, the expected response is:

[
  { 'c22d8560-2d42-45c0-9ca0-91a4eabd193d': 'Allowed capacity is 3' },
  { 'be63bd14-f448-4dc4-8087-3b895095d0bc': 'Allowed capacity is 2' },
];

Is it possible to achieve it?

CodePudding user response:

Code

const response = values
    .map((value) => {
        const allowedValue = allowedValues.find((allowedValue) => {
            return allowedValue.id === value.id;
        }) ?? { capacity: Infinity };
        return {
            ...value,
            allowedCapacity: allowedValue.capacity,
        };
    })
    .filter((value) => {
        return value.capacity > value.allowedCapacity;
    })
    .map((value) => {
        return {
            [value.id]: `Allowed capacity is ${value.allowedCapacity}`,
        };
    });

Explanation

Map the values to a list of objects that look like {id, capacity, allowedCapacity} and use Infinity for entries that don't have an allowed capacity. Then filter that to only the results where the capacity exceeds the max allowed capacity. Then map that result into the desired format where the keys are ids and the values are error messages containing the max capacity.

CodePudding user response:

1) You can easily achieve the result by first creating a Map if id and capacity

const dict = new Map(allowedValues.map(({ id, capacity }) => [id, capacity]));

2) Loop over values array using reduce to mach the id and get the result

const values = [
  {
    id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
    capacity: 8,
  },
  {
    id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
    capacity: 6,
  },
  {
    id: "djkfu4-xvvxvx-4344-sddsd",
    capacity: 1,
  },
];

const allowedValues = [
  {
    id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
    capacity: 3,
  },
  {
    id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
    capacity: 2,
  },
];

const dict = new Map(allowedValues.map(({ id, capacity }) => [id, capacity]));

const result = values.reduce((a, c) => {
  if (dict.has(c.id) && c.capacity > dict.get(c.id))
    a.push({ [c.id]: `Allowed capacity is ${dict.get(c.id)}` });
  return a;
}, []);

console.log(result);

CodePudding user response:

One more way with forEach :

const values = [
  {id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d", capacity: 8},
  {id: "be63bd14-f448-4dc4-8087-3b895095d0bc", capacity: 6},
  {id: "djkfu4-xvvxvx-4344-sddsd", capacity: 1}
]

const allowedValues = [
  { id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d", capacity: 3},
  { id: "be63bd14-f448-4dc4-8087-3b895095d0bc",capacity: 2}
]

let res = []
values.forEach(v => {
  allowedValues.forEach(a => {
    v.capacity > a.capacity && res.push({[a.id]: 'Allowed capacity is '   a.capacity }) 
  })
})
res = Array.from(new Set(res.map(JSON.stringify))).map(JSON.parse);
console.log(res)

CodePudding user response:

A straightforward and descriptive means of accomplishing this is to create a lookup table out of the allowedValues array, and then reduce the data array against that.

Here using a Map for the lookup, and a for...of loop to process the result.

const values = [ { id: 'c22d8560-2d42-45c0-9ca0-91a4eabd193d', capacity: 8, }, { id: 'be63bd14-f448-4dc4-8087-3b895095d0bc', capacity: 6, }, { id: 'djkfu4-xvvxvx-4344-sddsd', capacity: 1, }, ]; 
const allowedValues = [ { id: 'c22d8560-2d42-45c0-9ca0-91a4eabd193d', capacity: 3, }, { id: 'be63bd14-f448-4dc4-8087-3b895095d0bc', capacity: 2, }, ];

const avMap = new Map(allowedValues.map(({ id, capacity }) => [id, capacity]));

const result = [];
for (const { id, capacity } of values) {
  if (avMap.has(id) && capacity > avMap.get(id)) {
    result.push({ [id]: `Allowed capacity is ${avMap.get(id)}` });
  }
}

console.log(result);

  • Related