javascript 复制具有特定值的对象

8fq7wneg  于 2023-02-18  发布在  Java
关注(0)|答案(5)|浏览(100)

我有一个场景,我需要复制报价(没有价格),如果价格大于0从下面的json:

const data = [
    {
        "id": "offer1",
        "Identifier": {
            "info": "some info for offer1",
            "value": "some string for offer1"
        },
        "Price": {
            "Total": 94.30
        }
    },  
    {
        "id": "offer2",
        "Identifier": {
            "info": "some info for offer2",
            "value": "some string for offer2"
        },
        "Price": {
            "Total": 0.0
        }
    },
    {
        "id": "offer3",
        "Identifier": {
            "info": "some info for offer3",
            "value": "some string for offer3"
        },
        "Price": {
            "Total": 48.50
        }
    }
];

我需要获取所有价格〉0的报价,并将它们放入另一个具有特定格式的数组中,预期结果应如下所示:

const result = [
  {
    "id":"offer1",
    "reference":"offer1",
    "Identifier":{
      "info":"some info for offer1",
      "value":"some string for offer1"
    }
  },
  {
    "id":"offer3",
    "reference":"offer3",
    "Identifier":{
      "info":"some info for offer3",
      "value":"some string for offer3"
    }
  }
]

我做的第一步是过滤初始数据数组以排除Total = 0的报价。

const filteredOffers = data.filter(offer => offer.Price.Total > 0);

接下来我该怎么做才能得到预期的结果?谢谢。

nvbavucw

nvbavucw1#

可以Map新对象。

const
    data = [{ id: "offer1", Identifier: { info: "some info for offer1", value: "some string for offer1" }, Price: { Total: 94.3 } }, { id: "offer2", Identifier: { info: "some info for offer2", value: "some string for offer2" }, Price: { Total: 0 } }, { id: "offer3", Identifier: { info: "some info for offer3", value: "some string for offer3" }, Price: { Total: 48.5 } }],
    result = data
        .filter(({ Price: { Total } }) => Total > 0)
        .map(({ Price, ...o }) => ({ ...o, reference: o.id }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
vyswwuz2

vyswwuz22#

您可以使用reduce方法来Map这些项,并且只返回价格不等于0的项。

function formatOffers(data) {
  return data.reduce((acc, item) => {
    const { id, Identifier, Price } = item;

    if (Price.Total === 0) {
      return acc;
    } else {
      return [...acc, { id, reference: id, Identifier, Price }];
    }
  }, []);
}
vvppvyoh

vvppvyoh3#

下一步可以是:

const offersWithoutPrice = filteredOffers.map(offer => {
    delete offer["Price"]; 
    return offer;
});
jmo0nnb3

jmo0nnb34#

继续你的努力...
const filteredOffers = data.filter(offer => offer.Price.Total > 0);
你可以把过滤后的Map成这样的形状。

const newShape = filteredOffers.map(offer=>{
delete offer[price]
return offer;
})
56lgkhnf

56lgkhnf5#

您可以在mapfilter方法的帮助下实现所需的功能,例如:

const data = [
  {
    id: "offer1",
    Identifier: {
      info: "some info for offer1",
      value: "some string for offer1"
    },
    Price: {
      Total: 94.3
    }
  },
  {
    id: "offer2",
    Identifier: {
      info: "some info for offer2",
      value: "some string for offer2"
    },
    Price: {
      Total: 0.0
    }
  },
  {
    id: "offer3",
    Identifier: {
      info: "some info for offer3",
      value: "some string for offer3"
    },
    Price: {
      Total: 48.5
    }
  }
];

const filtredData = data
  .filter((d) => d.Price.Total > 0)
  .map((d) => {
    return { id: d.id, reference: d.id, Identifier: d.Identifier };
  });
console.log(filtredData);

相关问题