Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get default array filter object if it not exist in Javascript

I have an array:

const data = [
              {location: "Phnom Penh", sale: 1000 },
              {location: "Kandal", sale: 500 },
              {location: "Takeo", sale: 300 },
              {location: "Kompot", sale: 700 },
              {location: "Prey Veng", sale: 100 },
              {location: "Seam Reap", sale: 800 },
              {location: "Null", sale: 0}
            ];

and this is my function filter:

function getSale(data, arr) {
  return data
    .filter(el => arr.includes(el.location))
}
arr = getSale(data, ['Phnom Penh', 'AA', 'Kompot', 'BB']);
console.log(arr);
result: [{
    location: "Phnom Penh",
    sale: 1000
  },
  {
    location: "Kompot",
    sale: 700
  }
]

If 'AA' not found in filter I want it get the 'Null' Object.

My purpose I want the result like this:

 result: [
  {location: "Phnom Penh", sale: 1000},
  {location: "Null", sale: 0},
  {location: "Kompot", sale: 700},
  {location: "Null", sale: 0}
 ]

How I do? Thanks for help.

like image 643
KHEANG Youmay Avatar asked Mar 05 '20 07:03

KHEANG Youmay


2 Answers

You should rather use .map and .find:

const data = [
  { location: 'Phnom Penh', sale: 1000 },
  { location: 'Kandal', sale: 500 },
  { location: 'Takeo', sale: 300 },
  { location: 'Kompot', sale: 700 },
  { location: 'Prey Veng', sale: 100 },
  { location: 'Seam Reap', sale: 800 },
  { location: 'Null', sale: 0 },
];

function getSale(data, arr) {
  return arr.map(el => {
    const found = data.find(obj => obj.location === el);
    return found ? found : { location: 'Null', sale: 0 };
  });
}
arr = getSale(data, ['Phnom Penh', 'AA', 'Kompot', 'BB']);
console.log(arr);

Alternatively create a hash from data array and use .map directly:

const data = [
  { location: 'Phnom Penh', sale: 1000 },
  { location: 'Kandal', sale: 500 },
  { location: 'Takeo', sale: 300 },
  { location: 'Kompot', sale: 700 },
  { location: 'Prey Veng', sale: 100 },
  { location: 'Seam Reap', sale: 800 },
];

const hash = data.reduce((hash, obj) => {
  hash[obj.location] = obj;
  return hash;
}, {});
function getSale(data, arr) {
  return arr.map(el => hash[el] || { location: 'Null', sale: 0 });
}
arr = getSale(data, ['Phnom Penh', 'AA', 'Kompot', 'BB']);
console.log(arr);
like image 113
TheMaster Avatar answered Nov 02 '22 23:11

TheMaster


You could take a hash table for all locations and return either a known location or the one with Null as location.

const
    getSale = (data, locations) => {
        const sales = data.reduce((r, o) => (r[o.location] = o, r), {});
        return locations.map(l => sales[l] || sales.Null);
    },
    data = [{ location: "Phnom Penh", sale: 1000 }, { location: "Kandal", sale: 500 }, { location: "Takeo", sale: 300 }, { location: "Kompot", sale: 700 }, { location: "Prey Veng", sale: 100 }, { location: "Seam Reap", sale: 800 }, { location: "Null", sale: 0 }],
    locations = ['Phnom Penh', 'AA', 'Kompot', 'BB'],
    result = getSale(data, locations);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 37
Nina Scholz Avatar answered Nov 03 '22 00:11

Nina Scholz