将来自API调用的数据添加到我当前的对象中

我有一个看起来像这样的对象(比这个要大得多,但是我在缩短可读性)

{
  "listings": [
    {
      "id": "5e0f9c42e84d27001f63f725",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f93b",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f96a",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f999",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f9c8",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f9f7",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd0ff2ba100208700d0",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd1ff2ba1002087036c",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd1ff2ba100208703a1",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd1ff2ba100208703d6",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
  ]
}

我调用一个api来检索特定日期的日历数据,并且需要更新当前对象。我的问题是两部分-

  1. 如何仅更新从呼叫返回的列表
  2. 响应将随我在下面粘贴的内容一起返回。
[{ data: {date: "2020-04-29", listingId: "5e0f9c42e84d27001f63f725", status: "available", price: 95, …},
{ data: {date: "2020-04-30", listingId: "5e0f9c42e84d27001f63f725", status: "available", price: 96, …},
{ data: {date: "2020-05-01", listingId: "5e0f9c42e84d27001f63f725", status: "available", price: 99, …},
{ data: {date: "2020-04-29", listingId: "5e0f9c43e84d27001f63f93b", status: "available", price: 98, …},
{ data: {date: "2020-04-30", listingId: "5e0f9c43e84d27001f63f93b", status: "available", price: 99, …},
{ data: {date: "2020-05-01", listingId: "5e0f9c43e84d27001f63f93b", status: "available", price: 100, …}]

如您所见,根据我从API请求的日期数,即使对于相同的listingId,它也会将每个日期作为其自己的对象返回。例如,我需要在原始对象中更新,并用id: 5e0f9c42e84d27001f63f725列出以将前3个价格和日期保存为calendar的一部分。所以看起来像

  {
      "id": "5e0f9c42e84d27001f63f725",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": [{ "date: ____, price: ____ },
                  { "date: ____, price: ____ },
                  { "date: ____, price: ____ }]
    },
回答如下:

首先,您的日历是字符串而不是数组

let newlistings = listings.map(listing => {
    return {...listing, calendar:[]}
})

然后,迭代您的responseAPI

response.forEach(r => {
// check if the current data is existe in your listings
const indexListing = newListings.findIndex(listing => listing.id === r.data.listingId)
// if u find it, push in
if(indexListing !== -1) {
    listings.listings[indexListing].calendar.push(r.data);
}
})

将来自API调用的数据添加到我当前的对象中

我有一个看起来像这样的对象(比这个要大得多,但是我在缩短可读性)

{
  "listings": [
    {
      "id": "5e0f9c42e84d27001f63f725",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f93b",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f96a",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f999",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f9c8",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0f9c43e84d27001f63f9f7",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd0ff2ba100208700d0",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd1ff2ba1002087036c",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd1ff2ba100208703a1",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
    {
      "id": "5e0fadd1ff2ba100208703d6",
      "property": "ithaca",
      "unit": "classic-king",
      "calendar": "[]"
    },
  ]
}

我调用一个api来检索特定日期的日历数据,并且需要更新当前对象。我的问题是两部分-

  1. 如何仅更新从呼叫返回的列表
  2. 响应将随我在下面粘贴的内容一起返回。
[{ data: {date: "2020-04-29", listingId: "5e0f9c42e84d27001f63f725", status: "available", price: 95, …},
{ data: {date: "2020-04-30", listingId: "5e0f9c42e84d27001f63f725", status: "available", price: 96, …},
{ data: {date: "2020-05-01", listingId: "5e0f9c42e84d27001f63f725", status: "available", price: 99, …},
{ data: {date: "2020-04-29", listingId: "5e0f9c43e84d27001f63f93b", status: "available", price: 98, …},
{ data: {date: "2020-04-30", listingId: "5e0f9c43e84d27001f63f93b", status: "available", price: 99, …},
{ data: {date: "2020-05-01", listingId: "5e0f9c43e84d27001f63f93b", status: "available", price: 100, …}]

如您所见,根据我从API请求的日期数,即使对于相同的listingId,它也会将每个日期作为其自己的对象返回。例如,我需要在原始对象中更新,并用id: 5e0f9c42e84d27001f63f725列出以将前3个价格和日期保存为calendar的一部分。所以看起来像

  {
      "id": "5e0f9c42e84d27001f63f725",
      "property": "ithaca",
      "unit": "superior-king",
      "calendar": [{ "date: ____, price: ____ },
                  { "date: ____, price: ____ },
                  { "date: ____, price: ____ }]
    },
回答如下:

首先,您的日历是字符串而不是数组

let newlistings = listings.map(listing => {
    return {...listing, calendar:[]}
})

然后,迭代您的responseAPI

response.forEach(r => {
// check if the current data is existe in your listings
const indexListing = newListings.findIndex(listing => listing.id === r.data.listingId)
// if u find it, push in
if(indexListing !== -1) {
    listings.listings[indexListing].calendar.push(r.data);
}
})