通过从现有对象

时间:2019-01-16 19:01:23

标签: javascript json object

有一点背景,我从Angular 6组件中发布到PayPal的API并收到以下错误:

Error: Request to post https://www.sandbox.paypal.com/v1/payments/payment failed with 400 error. Correlation id: c4d516c6ef58b, c4d516c6ef58b

{
    "name": "VALIDATION_ERROR",
    "details": [
        {
            "field": "transactions.item_list.items.category",
            "issue": "Cannot construct instance of `com.paypal.platform.payments.model.rest.types.ItemCategory`, problem: oils is invalid value. Supported values are PHYSICAL, DIGITAL"
        }
    ],
    "message": "Invalid request - see details",
    "information_link": "https://developer.paypal.com/docs/api/payments/#errors",
    "debug_id": "c4d516c6ef58b"

这是因为我的JSON有一个名为“ category”的元素,但其值不是“ PHYSICAL”或“ DIGITAL”

结果,我需要创建一个包含PayPal可以接受的元素的新对象-id将成为sku,名称可以保留名称,价格可以保留价格,计数器将成为数量,描述将保持描述。

话虽如此,我收到的JSON对象比列出的五个对象具有更多的元素。我尝试通过以下方式制作另一个对象,但是收到了各种错误,从购物车未定义到.map不是函数:

this.product = this.shoppingCartObj.cart.products.product;
const Obj = this.product;
const countRows = Object.keys(Obj).map(function(key) {
  return [Number(key), Obj[key]];
});

for (let b = 0; b < countRows.length; b++) {
  this.copyShoppingCartObj.cart.products.product[
    b
  ].sku = this.shoppingCartObj.cart.products.product[b].id;
  this.copyShoppingCartObj.cart.products.product[
    b
  ].name = this.shoppingCartObj.cart.products.product[b].name;
  this.copyShoppingCartObj.cart.products.product[
    b
  ].quantity = this.shoppingCartObj.cart.products.product[b].counter;
  this.copyShoppingCartObj.cart.products.product[
    b
  ].price = this.shoppingCartObj.cart.products.product[b].price;
  this.copyShoppingCartObj.cart.products.product[
    b
  ].description = this.shoppingCartObj.cart.products.product[b].description;
  this.ar.push(this.product[b]);
}
this.product = this.copyShoppingCartObj.cart.products.product;

收件人:

this.copyShoppingCartObj = this.shoppingCartObj.map(data => ({
  sku: data.cart.products.product.id,
  name: data.cart.products.product.name,
  quantity: data.cart.products.product.counter,
  price: data.cart.products.product.price,
  description: data.cart.products.product.description
}));

而且-“ this.shoppingCartObj.forEach不是函数”

 this.shoppingCartObj.forEach(function(data) {
  const newMap = {};
  newMap.cart.products.product.sku = data.cart.products.product.id;
  newMap.cart.products.product.name = data.cart.products.product.name;
  newMap.cart.products.product.quantity =
    data.cart.products.product.counter;
  newMap.cart.products.product.price = data.cart.products.product.price;
  newMap.cart.products.product.description =
    data.cart.products.product.description;
  this.copyShoppingCartObj.push(newMap);
});

以下是JSON的示例:

[{
  "id": "8",
  "name": "Austin to Carls 32 to 33 chocolates to oil",
  "price": "9.67",
  "category": "oils",
  "description": "The standard Lorem Ipsum passage, used since the 1500s Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum. Section 1.10.32 of de Finibus Bonorum et Malorum, written by Cicero in 45 BC Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur ? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur",
  "ts": "2019-01-08 11:47:09",
  "product_image_id": "33",
  "enabled": "1",
  "product_image": "crater-winslow-40.png",
  "counter": 3
}, {
  "id": "9",
  "name": "something test 3 upd",
  "price": "12.23",
  "category": "chocolates",
  "description": "The standard Lorem Ipsum passage, used since the 1500s Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum. Section 1.10.32 of de Finibus Bonorum et Malorum, written by Cicero in 45 BC Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur ? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur",
  "ts": "2019-01-08 08:54:49",
  "product_image_id": "29",
  "enabled": "1",
  "product_image": "40-64-grand-canyon.png",
  "counter": 2
}]

预先感谢

1 个答案:

答案 0 :(得分:0)

您可以map遍历对象数组,并使用destructer获得desired key's value并根据您的key value对将它们作为对象返回。

let obj = [{"id": "8","name": "Austin to Carls 32 to 33 chocolates to oil","price": "9.67","category": "oils",  "description": "The standard Lorem Ipsum passage, used since the 1500s Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore e", "ts": "2019-01-08 11:47:09", "product_image_id": "33", "enabled": "1","product_image": "crater-winslow-40.png", "counter": 3}, {"id": "9","name": "something test 3 upd","price": "12.23","category": "chocolates","description": "The standard Lorem Ipsum passage, used since the 1500s Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididu", "ts": "2019-01-08 08:54:49", "product_image_id": "29", "enabled": "1", "product_image": "40-64-grand-canyon.png","counter": 2}]

let op = obj.map(e=>{
  let {counter,id,name,price,description} = e;
  return{
    name,price,description,
    quantity: counter,
    sku: id,
  }
})

console.log(op)