将对象数组转换为不同结构化对象的数组

时间:2019-10-17 15:31:44

标签: javascript arrays

我的问题可能很简单,但我的眼睛眨了眨眼,我现在无法思考其他问题。任何帮助表示赞赏

这是我的初始数组

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="MAIN | SUBMAIN">
    <xsl:variable name="n">
        <xsl:number/>
    </xsl:variable>
    <xsl:element name="{name()}{$n}">
        <xsl:apply-templates/>   
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

这是我需要的数组(更改名称并排除一些元素)

var Products = [
   {
     "order_product_id":"1111", 
     "order_id":"555", 
     "product_id":"111", 
     "name":"name1", 
     "model":"111", 
     "quantity":"1", 
     "price":"100", 
     "total":"100", 
     "tax":"20",
   },
   {      
     "order_product_id":"2222",    
     "order_id":"555",      
     "product_id":"222",      
     "name":"name2",      
     "model":"222",      
     "quantity":"1",      
     "price":"200",      
     "total":"200",      
     "tax":"40",      

   }
];

最后这是我要在处理后放置新产品系列的地方

{
      "id": "1111",
      "name": "name1",
      "category": "",
      "list_position": 1,
      "quantity": 1,
      "price": '100'
    },
    {
      "id": "2222",
      "name": "name2",
      "category": " ",
      "list_position": 2,
      "quantity": 1,
      "price": '200'
    }

我尝试了push / forEach /甚至字符串,但是我无法像Google那样拥有最终的干净数组

   {
    "transaction_id": 1,
    "value": 99,
    "currency": "EUR",
    "shipping": 0,
    "items": Items
   }

2 个答案:

答案 0 :(得分:2)

您可以创建自己的方法来构建对象并使用Array.prototype.map进行迭代。

var Products = [
  {
    order_product_id: "1111",
    order_id: "555",
    product_id: "111",
    name: "name1",
    model: "111",
    quantity: "1",
    price: "100",
    total: "100",
    tax: "20"
  },
  {
    order_product_id: "2222",
    order_id: "555",
    product_id: "222",
    name: "name2",
    model: "222",
    quantity: "1",
    price: "200",
    total: "200",
    tax: "40"
  }
];

let getObj = (obj, i) => ({
  id: obj.order_product_id,
  name: obj.name,
  category: obj.category || "",
  list_position: i + 1,
  quantity: obj.quantity,
  price: obj.price
});

let Items = Products.map(getObj);
console.log(Items);

答案 1 :(得分:1)

我已经遍历了产品列表,并为每个产品创建了一个商品,然后将其以新格式推送到商品清单中。

您在这里:

var products = [{
    "order_product_id": "1111",
    "order_id": "555",
    "product_id": "111",
    "name": "name1",
    "model": "111",
    "quantity": "1",
    "price": "100",
    "total": "100",
    "tax": "20",
  },
  {
    "order_product_id": "2222",
    "order_id": "555",
    "product_id": "222",
    "name": "name2",
    "model": "222",
    "quantity": "1",
    "price": "200",
    "total": "200",
    "tax": "40",

  }
];

let destinationObject = {
  "transaction_id": 1,
  "value": 99,
  "currency": "EUR",
  "shipping": 0,
  "items": []
};


products.forEach(function(element, index) {

  let item = {
    "id": element.order_product_id,
    "name": element.name,
    "category": "",
    "list_position": index,
    "quantity": element.quantity,
    "price": element.price
  };

  destinationObject.items.push(item);

});

console.log('destinationObject', destinationObject);