0

I have the below object which has array of objects. I want format this object to object of Array of arrays

var object1 = {
  "Usa": [{
    "period": "2018-11-03T00:00:00.000+0000",
    "qty": 1
  }, {
    "period": "2018-11-04T00:00:00.000+0000",
    "qty": 2
  }],
  "india": [
    {
      "period": "2018-19-03T00:00:00.000+0000",
      "qty": 2
    }, {
      "period": "2018-19-04T00:00:00.000+0000",
      "qty": 3
    }
  ]
}

export const createDataPoint = (period, qty) => {
  return [
    Date.now(time),
    Math.round((Math.random() * 100) * 2) / 2 + quantity,

  ];
};

export function createData(object1) {
  let data = [];
  let total = [];
  Object.keys(object1).map(function(item, index) {
    object1[item].map(function(item2, index2) {
      data.push(createDataPoint(item2.period, item2.qty));
    })
    object1[item] = data
  })
  // console.log(object1)
  return object1;
}

But the output is, in every array there are 4 arrays instead of 2 respective arrays. Which means in every array, i am getting total arrays of size 4.

Expected output is

  var object1={
     "Usa":[
      ["123245235",1],
      ["21423435",2]
     ],
    "india":[   
       ["234325436",2],
        ["23422464",3]
    ]    
  }

3 Answers 3

1

Just reassign each array to new mapped array

const createDataPoint = ({period, qty})=>{
    // do your processing here, just returning period & qty for simplicity 
    return [period, qty]
}

Object.keys(data).forEach(k=>{
    data[k] = data[k].map(createDataPoint);
})

console.log(data)
<script>
  const data = {
    "Usa": [{
      "period": "2018-11-03T00:00:00.000+0000",
      "qty": 1
    }, {
      "period": "2018-11-04T00:00:00.000+0000",
      "qty": 2
    }],
    "india": [{
        "period": "2018-19-03T00:00:00.000+0000",
        "qty": 2
      }, {
        "period": "2018-19-04T00:00:00.000+0000",
        "qty": 3
      }

    ]
  }
</script>

Sign up to request clarification or add additional context in comments.

Comments

1

This is an alternative using the function Object.entries to get the entries as follow [country, array] and then with the function reduce build the desired output.

The function map will generate the array with tow indexes.

This approach doesn't mutate the original object

var object1={  "Usa":[       {"period": "2018-11-03T00:00:00.000+0000",               "qty": 1           }, {               "period": "2018-11-04T00:00:00.000+0000",               "qty": 2           } ], "india":[   {    "period": "2018-19-03T00:00:00.000+0000",               "qty": 2           }, {               "period": "2018-19-04T00:00:00.000+0000",               "qty": 3           } ]    },
    result = Object.entries(object1).reduce((a, [country, arr]) => {
      return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
    }, Object.create(null));

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

Comments

1

You can use Array.prototype.reduce & Object.entries:

var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }

const result = Object.entries(obj)
  .reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)

Somewhat simple approach can be done via Object.keys & reduce:

var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }

const result = Object.keys(obj)
  .reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.