Bigboss Highlight
Bigboss Highlight

Reputation: 33

How to loop an object and get specific value to push an array each

I've got some case to loop an object which has multiple keys and get specific value and push to an array each, I hope anyone in this forum can help me thanks

[{
  "total_sms": 887,
  "total_submitted": 101,
  "total_in_queue": 696,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}, {
  "total_sms": 888,
  "total_submitted": 102,
  "total_in_queue": 697,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}]

expected

total_sms = [887,888]
total_submitted = [101,102]
and etc

Upvotes: 3

Views: 110

Answers (6)

Bob White
Bob White

Reputation: 733

var temp = [{
  "total_sms": 887,
  "total_submitted": 101,
  "total_in_queue": 696,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}, {
  "total_sms": 888,
  "total_submitted": 102,
  "total_in_queue": 697,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}
]

var obj = {

}

 temp.forEach(item => {
   Object.keys(item).map(key => {
     obj[key] = (obj[key]||[]).concat(item[key])
   })
 })

 console.log(obj)

Upvotes: 1

Neel Rathod
Neel Rathod

Reputation: 2111

If you want a single element data then map() will help you

const array = [{
  total_sms: 887,
  total_submitted: 101,
  total_in_queue: 696,
  total_processed: 0,
  total_delivered: 0,
  total_failed: 0,
  date: '2019-08-06',
}, {
  total_sms: 888,
  total_submitted: 102,
  total_in_queue: 697,
  total_processed: 0,
  total_delivered: 0,
  total_failed: 0,
  date: '2019-08-06',
}];


const sms = array.map((m) => { return m.total_sms; });
console.log(sms);

If you want to all keys into an array then you can achieve it by only one for loop

  const array = [{
    total_sms: 887,
    total_submitted: 101,
    total_in_queue: 696,
    total_processed: 0,
    total_delivered: 0,
    total_failed: 0,
    date: '2019-08-06',
  }, {
    total_sms: 888,
    total_submitted: 102,
    total_in_queue: 697,
    total_processed: 0,
    total_delivered: 0,
    total_failed: 0,
    date: '2019-08-06',
  }];
  
  const result = {};
  
  for (let i = 0; i < array.length; i += 1) {
    const element = array[i];
    Object.keys(element)
      .forEach((subElem) => {
        if (result[subElem]) {
          result[subElem].push(element[subElem]);
        } else {
          result[subElem] = [element[subElem]];
        }
      });
  }
  
  console.log(result);

Upvotes: 0

Taki
Taki

Reputation: 17654

Use Array.reduce staring with an object having empty arrays and use for .. in inside it to fill that initial object from the current onw in the loop :

var temp = [
  {
    total_sms: 887,
    total_submitted: 101,
    total_in_queue: 696,
    total_processed: 0,
    total_delivered: 0,
    total_failed: 0,
    date: "2019-08-06"
  },
  {
    total_sms: 888,
    total_submitted: 102,
    total_in_queue: 697,
    total_processed: 0,
    total_delivered: 0,
    total_failed: 0,
    date: "2019-08-06"
  }
];

const result = temp.reduce(
  (all, curr) => {
    for (let k in curr) {
      all[k].push(curr[k]);
    }
    return all;
  },
  {
    total_sms: [],
    total_submitted: [],
    total_in_queue: [],
    total_processed: [],
    total_delivered: [],
    total_failed: [],
    date: []
  }
);

console.log(result);

Upvotes: 0

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92347

Try

data.forEach(d => Object.keys(d).forEach(k=> r[k]=(r[k]||[]).concat(d[k]) ));

let data = [{
  "total_sms": 887,
  "total_submitted": 101,
  "total_in_queue": 696,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}, {
  "total_sms": 888,
  "total_submitted": 102,
  "total_in_queue": 697,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}];

let r={};

data.forEach(d => Object.keys(d).forEach(k=> r[k]=(r[k]||[]).concat(d[k]) ));

Object.keys(r).forEach(k=> this[k]=r[k] ); // save as global variables

console.log('total_sms =', total_sms);
console.log('total_submitted =', total_submitted);
console.log('...')

Upvotes: 0

junvar
junvar

Reputation: 11574

let objs = [{
  "total_sms": 887,
  "total_submitted": 101,
  "total_in_queue": 696,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}, {
  "total_sms": 888,
  "total_submitted": 102,
  "total_in_queue": 697,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}];

let arrays = objs.reduce((arrays, obj) => {
  Object.entries(obj).forEach(([key, value]) => {
    arrays[key] = arrays[key] || [];
    arrays[key].push(value);
  });
  return arrays;
}, {});

console.log(arrays);

Upvotes: 2

Fullstack Guy
Fullstack Guy

Reputation: 16908

You can use Array.reduce to group by the keys of each object in your array. The keys of each object can be obtained from Object.keys.

Now for each key in the object if it is present then just add it to the array else create a new array and add that element:

const data = [{
  "total_sms": 887,
  "total_submitted": 101,
  "total_in_queue": 696,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}, {
  "total_sms": 888,
  "total_submitted": 102,
  "total_in_queue": 697,
  "total_processed": 0,
  "total_delivered": 0,
  "total_failed": 0,
  "date": "2019-08-06"
}];

const grouped = data.reduce((acc, ele) => {
    const keys = Object.keys(ele);
    keys.forEach(key => acc[key] = acc[key] ? acc[key].concat(ele[key]) : [ele[key]]);
    return acc;
 }, {});
 console.log(grouped);
    

Upvotes: 4

Related Questions