Home > Software design >  Merge & get then push to one array
Merge & get then push to one array

Time:12-02

I have data like this:

let data = []
let weeks = [
                            {
                                "week": 1,
                                "crop": {
                                    "aop": 1,
                                    "actual": 2,
                                    "kbm": 3
                                },
                                "hk": {
                                    "aop": 4,
                                    "actual": 5,
                                    "kbm": 6
                                },
                                "outputHkAct": 7
                            },
                            {
                                "week": 2,
                                "crop": {
                                    "aop": 11,
                                    "actual": 12,
                                    "kbm": 13
                                },
                                "hk": {
                                    "aop": 14,
                                    "actual": 15,
                                    "kbm": 16
                                },
                                "outputHkAct": 17
                            },
                        ]

i wanth to merge & get the value every week in crop(aop, actual, kbm), outputHkAct , hk(aop, actual, kbm), outputHkAct. the output should be :

  data =  [1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17]

anyone can help? thanks guys.

CodePudding user response:

It can be solved by using forEach:

let data = []
let weeks = [
    {
         "week": 1,
         "crop": {
              "aop": 1,
              "actual": 2,
              "kbm": 3
         },
         "hk": {
             "aop": 4,
             "actual": 5,
             "kbm": 6
         },
         "outputHkAct": 7
     },
     {
         "week": 2,
         "crop": {
              "aop": 11,
              "actual": 12,
              "kbm": 13
         },
         "hk": {
             "aop": 14,
             "actual": 15,
             "kbm": 16
         },
         "outputHkAct": 17
    },
]

weeks.forEach(({crop,hk,outputHkAct})=>{
    data.push(crop.aop);
    data.push(crop.actual);
    data.push(crop.kbm);
    data.push(hk.aop);
    data.push(hk.actual);
    data.push(hk.kbm);
    data.push(outputHkAct)
})

console.log(data)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can suer for in loop which helps you to loop through an object

let weeks = [
    {
         "week": 1,
         "crop": {
              "aop": 1,
              "actual": 2,
              "kbm": 3
         },
         "hk": {
             "aop": 4,
             "actual": 5,
             "kbm": 6
         },
         "outputHkAct": 7
     },
     {
         "week": 2,
         "crop": {
              "aop": 11,
              "actual": 12,
              "kbm": 13
         },
         "hk": {
             "aop": 14,
             "actual": 15,
             "kbm": 16
         },
         "outputHkAct": 17
    },
]
let allValues = [];
weeks.map(obj=>{
    for(let key in obj){
      if(key === 'crop' || key === 'hk'){
        for(let k in obj[key]){
          allValues.push(obj[key][k])
        }
      }else if(key === 'outputHkAct'){
        allValues.push(obj[key])
      }
    }
})


console.log(allValues)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

weeks is an array of object. So you have to iterate through weeks array (by using for-of, for-in, while, map, reduce, etc.) to get the object and now you can access the value of the object by using a key which you already know (crop(aop, actual, kbm), outputHkAct , hk(aop, actual, kbm), outputHkAct).

Here is the example by using reduce.

let weeks = [
{
    "week": 1,
    "crop": {
        "aop": 1,
        "actual": 2,
        "kbm": 3
    },
    "hk": {
        "aop": 4,
        "actual": 5,
        "kbm": 6
    },
    "outputHkAct": 7
},
{
    "week": 2,
    "crop": {
        "aop": 11,
        "actual": 12,
        "kbm": 13
    },
    "hk": {
        "aop": 14,
        "actual": 15,
        "kbm": 16
    },
    "outputHkAct": 17
},
];

let data = [...weeks.reduce((v, { crop, hk, outputHkAct }) => [...v, ...Object.values(crop), ...Object.values(hk), outputHkAct], [])];

console.log(data);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related