0

I have a json array as follows , this data is from an excel sheet and converted it to json format.

[{
  'booktitle': 'Leading',
  'bookid': '56353',
  'bookauthor': 'Sir Alex Ferguson'
}, {
  'booktitle': 'How Google Works',
  'bookid': '73638',
  'bookauthor': 'Eric Smith'
}, {
  'booktitle': 'The Merchant of Venice',
  'bookid': '37364',
  'bookauthor': 'William Shakespeare'
}]

I would like to get value with same key in one array. This key value pair will be dynamic so want a solution in general.

Beat
  • 4,386
  • 2
  • 35
  • 52
Anuja vinod
  • 99
  • 3
  • 11
  • What is the key here can you explain little more clearly – Deepak Kumar T P Dec 19 '17 at 06:05
  • 1
    What have you tried? Post your attempt. – vibhor1997a Dec 19 '17 at 06:06
  • [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Dec 19 '17 at 06:08
  • How to group array of objects by key :this example is similar, but my key value pair will be dynamic, in this case booktitle,bookid,bookauthor are the keys but in others cases it will be different and the count will also be different so how get values if we don't know the key in json array – Anuja vinod Dec 19 '17 at 06:16

3 Answers3

1

I think what you are looking for is this. If you want to add anything else, please let me know. Check the code snippet.

const obj = [{
  'booktitle': 'Leading',
  'bookid': '56353',
  'bookauthor': 'Sir Alex Ferguson'
}, {
  'booktitle': 'How Google Works',
  'bookid': '73638',
  'bookauthor': 'Eric Smith'
}, {
  'booktitle': 'The Merchant of Venice',
  'bookid': '37364',
  'bookauthor': 'William Shakespeare'
}];

const res = obj.reduce((a, b) => {
  for(let i in b) {
    if(!a[i]) {
      a[i] = [];
    }
    a[i].push(b[i]);
  }
  
  return a;
}, {});

console.log(res);
Shubham
  • 1,755
  • 3
  • 17
  • 33
0
let bookInfo = [{
  'booktitle': 'Leading',
  'bookid': '56353',
  'bookauthor': 'Sir Alex Ferguson'
}, {
  'booktitle': 'How Google Works',
  'bookid': '73638',
  'bookauthor': 'Eric Smith'
}, {
 'booktitle': 'The Merchant of Venice',
 'bookid': '37364',
 'bookauthor': 'William Shakespeare'
}]

  var bookTitle = [];
  var bookId   = [];
  var bookAuthor = [];

 bookInfo.filter(function(data){
      if(data.booktitle){
          bookTitle.push(data.booktitle);
       }

       if(data.bookid){
          bookId.push(data.bookid);
       }


       if(data.bookauthor){
          bookAuthor.push(data.bookauthor);
       }

 })
aabiskar
  • 654
  • 9
  • 24
0

You can do this using Array map Object keys like this,

let arr = [{
    "booktitle": "Leading",
    "bookid": "56353",
    "bookauthor": "Sir Alex Ferguson"
}, {
    "booktitle": "How Google Works",
    "bookid": "73638",
    "bookauthor": "Eric Smith"
}, {
    "booktitle": "The Merchant of Venice",
    "bookid": "37364",
    "bookauthor": "William Shakespeare"
}];

let ans=Object.keys(arr[0]).map((key) => {
    let o={};
    o[key]=arr.map((x) => x[key]);
    return o;
});

console.log(ans);
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37