使用新对象中的值更新原始对象中的值,如果不存在则将其设置为0

时间:2019-09-04 21:17:50

标签: javascript arrays object merge

我有2个对象。 filter和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
    }
  ]
};

需要使用newFilters的doc_count更新过滤器中的doc_values值。返回一个新对象。

这部分我正在工作。看到这里:

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

但是,它将更新所有现有值,并保持原样不变。这是预期的。但就我而言,如果没有要更新的内容,我需要将其设置为doc_count: 0

例如:在该示例中,它返回roomType并按预期更新了“私人房间”和“整套家庭/公寓”,但

key: "Shared room" doc_count: 6

应该是

key: "Shared room" doc_count: 0

这是因为如果不返回任何更新,则没有可用的项目。

它应该像这样:

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
    }
  ]
};

2 个答案:

答案 0 :(得分:1)

怎么样呢?

handleSearch = query => {
    this.setState({ searchQuery: query });
    this.getPagedData();
  };

  getPagedData = () => {
    const { searchQuery, events: allEvents } = this.state;

    let filtered = allEvents;
    if (searchQuery) {
      filtered = allEvents.filter(
        e =>
          e.title.toLowerCase().startsWith(searchQuery.toLowerCase()) ||
          e.hostName.toLowerCase().startsWith(searchQuery.toLowerCase())
      );
    }

    if (searchQuery.length === 0 || searchQuery.length === 1) {
      this.setState({
        events: getEvents()
      });
    } else {
      this.setState({
        events: filtered
      });
    }

    return { totalCount: filtered.length };
  };

此处的沙盒:https://codesandbox.io/s/proud-haze-n4vsu

更新:上面返回一个数组,这是一个返回对象的版本:

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

此处更新了沙盒:https://codesandbox.io/s/dry-pine-oxpvf

答案 1 :(得分:1)

尝试一下:

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