简体   繁体   中英

Update values in original object with values from new object and set value to 0 if doesn't exist

I have 2 objects. filter and newFilters

const filters = {
  city: [
    {
      key: "Brooklyn",
      doc_count: 230
    },
    {
      key: "New York",
      doc_count: 224
    },
    {
      key: "Queens",
      doc_count: 18
    },
    {
      key: "Bronx",
      doc_count: 6
    },
    {
      key: "Staten Island",
      doc_count: 5
    },
    {
      key: "Long Island City",
      doc_count: 3
    },
    {
      key: "Rockaway Beach",
      doc_count: 1
    }
  ],
  roomType: [
    {
      key: "Entire home/apt",
      doc_count: 276
    },
    {
      key: "Private room",
      doc_count: 205
    },
    {
      key: "Shared room",
      doc_count: 6
    }
  ]
};

const newFilters = {
  city: [
    {
      key: "Bronx",
      doc_count: 6
    }
  ],
  roomType: [
    {
      key: "Private room",
      doc_count: 4
    },
    {
      key: "Entire home/apt",
      doc_count: 2
    }
  ]
};

The doc_count values in filter need to be updated with the doc_values of the newFilters. Returning a new object.

This part I have working. See here:

https://codesandbox.io/s/suspicious-raman-k6x2f

However, it is updating all existing values and leaving unchanged values as is. This is expected. But in my case I need it to set doc_count: 0 if there is nothing to update.

eg: In the example it returns roomType it updates Private room and Entire home/apt as expected but

key: "Shared room" doc_count: 6

This should be

key: "Shared room" doc_count: 0

This is because if it returns no update there are no items available.

It should look like this:

const newObject = {
  city: [
    {
      key: "Brooklyn",
      doc_count: 0
    },
    {
      key: "New York",
      doc_count: 0
    },
    {
      key: "Queens",
      doc_count: 0
    },
    {
      key: "Bronx",
      doc_count: 6
    },
    {
      key: "Staten Island",
      doc_count: 0
    },
    {
      key: "Long Island City",
      doc_count: 0
    },
    {
      key: "Rockaway Beach",
      doc_count: 0
    }
  ],
  roomType: [
    {
      key: "Entire home/apt",
      doc_count: 276
    },
    {
      key: "Private room",
      doc_count: 205
    },
    {
      key: "Shared room",
      doc_count: 0
    }
  ]
};

How about something like this:

const newObj = Object.keys(filters).map(filterKey => {
  const oldFilter = filters[filterKey];
  const newFilter = newFilters[filterKey];
  return oldFilter.map(f => {
    const matchingNewFilter = newFilter.find(nf => nf.key === f.key);
    return matchingNewFilter || { ...f, doc_count: 0 };
  });
});

Sandbox here: https://codesandbox.io/s/proud-haze-n4vsu

Update : the above returns an array, here is a version that returns an object:

const update = (...args) =>
  Object.keys(args[0]).reduce((acc, k) => {
    acc[k] = args[0][k].map(
      f =>
        args[1][k].find(nf => nf.key === f.key) || {
          ...f,
          doc_count: 0
        }
    );
    return acc;
  }, {});

console.log(update(filters, newFilters));

Updated sandbox here: https://codesandbox.io/s/dry-pine-oxpvf

Try this:

function update(filters, newFilters) {
  const combinedFilters = {};
  for (const category in filters) {
      combinedFilters[category] = [];
      filters[category].forEach((filter, filterIdx) => {
          const newFilter = newFilters[category].find(newFilter => {
              return newFilter.key === filter.key;
          });
          if (newFilter) {
              // Filter exists in newFilters, update filters with it
              combinedFilters[category].push(newFilter);
          } else {
              // Filter doesn't exist in newFilters so set the doc_count to 0
              combinedFilters[category].push({
                key: filter.key,
                doc_count: 0
              })
          }
      });
  }
  return combinedFilters;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM