将父数组元素和儿童数组推入JavaScript中的新数组

时间:2018-06-20 04:59:08

标签: javascript typescript react-native

"items": {
"hotdrinks": [
  {
    "id": "9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
    "price": 20,
    "name": "Tea",
    "img": "../assets/img/HotDrinks/1_udupibhavan.jpg"
  },
  {
    "id": "9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
    "price": 25,
    "name": "Coffee",
    "img": "../assets/img/Hot Drinks/2_udupibhavan.jpg"
  },
  {
    "id": "9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
    "price": 50,
    "name": "Hot Milk",
    "img": "../assets/img/Hot Drinks/3_udupibhavan.jpg"
  },
  {
    "id": "9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
    "price": 70,
    "name": "Horlicks",
    "img": "../assets/img/Hot Drinks/4_udupibhavan.jpg"
  },
  {
    "id": "9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
    "price": 80,
    "name": "Badam Milk",
    "img": "../assets/img/Hot Drinks/5_udupibhavan.jpg"
  }
],

}

json我想使用javascript实现。我只是处理数组和对象的新手。谢谢找到Jeeva给出的答案,效果很好 欢迎将来提供答案,因为我们可以知道实现同一json对象的不同方法

 dataArray = [
 {title:"Hotdrinks",
 content: [{
        "id": "9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
        "price": 20,
        "name": "Tea",
        "img": "../assets/img/HotDrinks/1_udupibhavan.jpg"
      },
      {
        "id": "9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
        "price": 80,
        "name": "Badam Milk",
        "img": "../assets/img/Hot Drinks/5_udupibhavan.jpg"
      }
]}

2 个答案:

答案 0 :(得分:1)

您可以像这样使用。这可以通过迭代对象来实现。

const data = {
              "items":{
                "hotdrinks":[
                  {
                  "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
                  "price":20,
                  "name":"Tea",
                  "img":"../assets/img/HotDrinks/1_udupibhavan.jpg"
                  },
                  {
                  "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
                  "price":25,
                  "name":"Coffee",
                  "img":"../assets/img/Hot Drinks/2_udupibhavan.jpg"
                  },
                  {
                  "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
                  "price":50,
                  "name":"Hot Milk",
                  "img":"../assets/img/Hot Drinks/3_udupibhavan.jpg"
                  },
                  {
                  "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
                  "price":70,
                  "name":"Horlicks",
                  "img":"../assets/img/Hot Drinks/4_udupibhavan.jpg"
                  },
                  {
                  "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
                  "price":80,
                  "name":"Badam Milk",
                  "img":"../assets/img/Hot Drinks/5_udupibhavan.jpg"
                  }
                ]
              }
            }
            

var dataArray = []

for(k in data.items){
  var dataObj = {}
  dataObj.title = k
  dataObj.content = data.items[k]  //You can also access the object values by using bracket ([]) notation
  dataArray.push(dataObj)
  
}

console.log(JSON.stringify(dataArray))            

答案 1 :(得分:0)

以上预期的输出json无效。我们可以实现以下目标。

[{"title":"Hotdrinks"}, {"content": [  
     {  
        "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
        "price":20,
        "name":"Tea",
        "img":"../assets/img/HotDrinks/1_udupibhavan.jpg"
     },
     {  
        "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
        "price":25,
        "name":"Coffee",
        "img":"../assets/img/Hot Drinks/2_udupibhavan.jpg"
     },
     {  
        "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
        "price":50,
        "name":"Hot Milk",
        "img":"../assets/img/Hot Drinks/3_udupibhavan.jpg"
     },
     {  
        "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
        "price":70,
        "name":"Horlicks",
        "img":"../assets/img/Hot Drinks/4_udupibhavan.jpg"
     },
     {  
        "id":"9aa113b4-1e4e-4cde-bf9d-8358fc78ea4f",
        "price":80,
        "name":"Badam Milk",
        "img":"../assets/img/Hot Drinks/5_udupibhavan.jpg"
     }
  ]}]

如果您对此表示满意,那么我将给您同样的示例代码。